A Perl beginner's array searcher

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
devdavad
Forum Newbie
Posts: 2
Joined: Wed Sep 03, 2008 11:08 pm

A Perl beginner's array searcher

Post by devdavad »

delete this account
Last edited by devdavad on Sat Jul 11, 2009 11:08 pm, edited 1 time in total.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: A Perl beginner's array searcher

Post by andyhoneycutt »

I may be wrong but I believe all that your elsif will do is compare the stdin input to whatever is in the last piece of your @something array.

It looks like what you are doing is saying if the key exists (and the user is searching for it), do something. Otherwise if the key equals the last item in the array (which should never trigger, because the first if will trigger: it exists), do something else.

Are you trying to allow a user to search until all keys are found? If you're just trying to loop through all possible items in the array, a foreach should suffice.

-Andy
devdavad
Forum Newbie
Posts: 2
Joined: Wed Sep 03, 2008 11:08 pm

Re: A Perl beginner's array searcher

Post by devdavad »

andyhoneycutt wrote:I may be wrong but I believe all that your elsif will do is compare the stdin input to whatever is in the last piece of your @something array.

It looks like what you are doing is saying if the key exists (and the user is searching for it), do something. Otherwise if the key equals the last item in the array (which should never trigger, because the first if will trigger: it exists), do something else.

Are you trying to allow a user to search until all keys are found? If you're just trying to loop through all possible items in the array, a foreach should suffice.

-Andy
Thanks. I'm trying to allow a user to search until all keys are found and having the stdin be a regex without /'s.

And I'm stupid for putting this in a PHP forum.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: A Perl beginner's array searcher

Post by andyhoneycutt »

not stupid at all. maybe a little misguided for putting it under coding critique, however: this particular forum, i believe, is designated for people asking other people to tear apart their code, make suggestions for optimizations and whatnot.

as far as your code goes, my suggestion would be to pop off a key from the array (and it's value, of course) after each time a user finds the key; do this until there is no more array.

-Andy
Post Reply