Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Loop once on condition 1, many times on cond. 2?

by punch_card_don (Curate)
on Oct 09, 2008 at 14:54 UTC ( [id://716226]=perlquestion: print w/replies, xml ) Need Help??

punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:

Mondo Monks,

What is the cleanest, most simple, way to do this:

If x='key_1', execute the following block of code for every key in %my_hash. But if x != 'key_1', execute that block of code for just that single key of %my_hash.

For example, this would do it, but it's clunky:

%my_hash = ( key_1 => $value_1, key_2 => $value_2, key_3 => $value_3, key_4 => $value_4, key_5 => $value_5 ); foreach $key (keys %my_hash) { if ($x eq 'key_1' || $key eq $x) { do a bunch of stuff; } }

Thanks




Time flies like an arrow. Fruit flies like a banana.

Replies are listed 'Best First'.
Re: Loop once on condition 1, many times on cond. 2?
by jethro (Monsignor) on Oct 09, 2008 at 15:08 UTC
    foreach $key ( ($x eq 'key_1') ? keys %my_hash : $x ) { do a bunch of stuff }

    UPDATE: Had to reverse the logic of the ?:, thought you wanted it the other way round

      That's certainly much tidier notation.

      The problem with mine is that it loops through all keys no matter what- it just doesn't execute the block of code when not necessary. What I was hoping for is a formula that only loops once when one loop is all that's needed. But I think your tidy notation functions the same as mine, no?




      Time flies like an arrow. Fruit flies like a banana.
        No.

        The expression is evaluated before the loop starts in order to build the list of items over which to iterate,
        and the expression returns either the result of keys or a single scalar depending on whether conditional's condition is true,
        so it either loops for every key or just once.

        No, because the ?: delivers either a list consisting of the single value or all the keys to the 'foreach' loop statement . So if that is only one value, the loop executes only for that single value.

Re: Loop once on condition 1, many times on cond. 2?
by apl (Monsignor) on Oct 09, 2008 at 17:31 UTC
    I'm a worry-wart about later needed changes. So rather than going for terser, I'd write:
    if ($x eq 'key_1') { DoaBunchOfStuff( $x ); } else { foreach $key (keys %my_hash) { DoaBunchOfStuff( $key ); } }

      In that case you should choose one of the other options so that you don't have to make changes to the call to DoaBunchOfStuff in multiple places - changing the parameter list for example. Repeating the "body" in that fashion also makes it less clear from the flow of the code that the two pieces of controlled work are in fact the same.

      In particular, constructing the list to process up front makes it very clear where the processing differences are and what conditions those differences depend on (AnomalousMonk's solution for example).


      Perl reduces RSI - it saves typing

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-28 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found