in reply to [untitled node, ID 216639]

There are good answers to your general question above, but one line jumped out at me: if (($i == 7) | ($i == 14) | ($i == 21) | ($i == 28) | ... ){ The first thing to jump out is the use of a bitwise or instead of a logical or. Correcting that leaves if (($i == 7) || ($i == 14) || ($i == 21) || ($i == 28) || ... ) { which still has a problem: unecessary parenthesis creating single-elements lists, each containing a boolean. Correcting this leaves if ( $i == 7 || $i == 14 || $i == 21 || $i == 28 || ... ) { This is correct, but a cursory glance shows that the intent is determine whether $i is a multiple of 7 (or, said another way, that $i is divisible by 7 without remainder). There's an easy way to express that: if ( $i % 7 == 0 ) { Phrased this way, you can increase the upper bound on your loop safely without having to add another condition to if if statement. Anytime you have to change code in more than one place to increase the range of a loop, a flag should go up.

Replies are listed 'Best First'.
Re: Re: Variables in variable names
by rir (Vicar) on Nov 30, 2002 at 21:44 UTC
    if (($i == 7) || ($i == 14) || ($i == 21) || ($i == 28) || ... ) {
    which still has a problem: unecessary parenthesis creating single-elements lists, each containing a boolean.

    This is wrong. Context creates the list not parentheses. Consider the following code. All the print statements are executed.

    my @ar = ( 0); # Could be: @ar = 0; no precedence problem unless ( 0 ) { print "0 is false!\n"; } unless ( (0) ) { print "(0) is false! But a list w/ 1 item is true.\n"; } if ( @ar ) { print "\@ar, non-empty array is true!\n"; } unless ( list_context() ) { print "scalar context!\n"; } sub list_context { return 1 if wantarray; return; }
    ~