Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Looking Through Arrays?

by djantzen (Priest)
on Dec 18, 2002 at 02:43 UTC ( [id://220740]=note: print w/replies, xml ) Need Help??


in reply to Looking Through Arrays?

As others already said, strict and warnings would have stopped you in your tracks long ago. In any case, how about this (taking some liberties with your variable names and assuming your subs are defined already):

my $member_call = 'Captain'; # or something my @allowed_ranks = qw/Recruit General Captain Civilian Airman/; grep($_ eq $member_call, @allowed_ranks) ? Welcome() : NotWelcome(); # + using grep+ternary operator

Update: further explanation to address FamousLongAgo's concerns. qw takes a list of strings and allows them to be specified without commas or quotation marks delimiting each. It's nice to use whenever you've got more than a few entries. grep is a function that iterates through a list, setting the global variable $_ to the current element of the list. It evaluates either an expression (as here) or a block, and returns a list of elements from the original list for which the expression was true. Now the ternary operator is short hand for "if x, then y, else z" where "?" means "if" and ":" means "else". Thus, we test to see if grep returned anything, and if so, call Welcome. Otherwise, call NotWelcome.

Hope that helps, fever

Replies are listed 'Best First'.
Re: Re: Looking Through Arrays?
by FamousLongAgo (Friar) on Dec 18, 2002 at 02:59 UTC
    I worry about posts in this vein ( not to pick on fever, there have been many, by various monks, including myself ), where a question that is clearly from a beginner elicits a dense piece of minimal code. In this case we get grep, qw, implicit assignment to $_ and the ternary operator, all in three lines.

    I'm not against offering challenging answers to help petitioners learn, but challenging should not mean off-putting, right?

    To be fair, perhaps my perception of the petitioner's level is not the same as fever's.

      I agree, far too much code - 3 lines where one will do

      sub check { grep{/^\Q$_[0]\E$/}qw/Recruit General Captain Civilian Airman/ ? W +elcome() : NotWelcome(); }

      or perhaps that was not what you meant... ;-)

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Heh, I've never been called a programming showoff before, thanks! But look, he'd already received two helpful answers that were sufficient to set him on the right track. True, my answer is not appropriate for a newbie, but there is plenty of precedent for other monks discussing particular problems at higher levels of difficulty once the petitioner has been assisted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://220740]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2025-06-20 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.