Jay Savage schreef:
> Dr.Ruud:
>> Jay Savage:

>>>     for (0..#$arrayname) { print $array[$_] }
>>
>> The "#$" should be "$#" and you have two different array names there
>> that I assume you meant equal.
>
> Not enough coffee this morning, I guess, but the point was to
> illustrate, not give working code. I think OP got it.
>
>> Alternative 1:
>>
>>   print $ary[$_] for $[ .. $#ary;
>
> I suppose it's an alternative, but why replace a constant with a
> variable if you don't have to? The only reason to do that would be if
> you messed with $[. Don't do that. Ever. Ever ever. At least not in
> any situation that's likely to get discussed on a beginners' list.

Why gamble that "0" is a valid index, or even the minimal index, if
there is "$["?

Of course "the others" should use a local "$[" if they change it from
the default 0 to some other value, but you just can't be sure that it
will never bite you. Unless you use "$[" with "$#ary", because "$#ary"
depends on "$[" like this: ($#ary -$[ +1) == scalar(@ary).


>> Alternative 2:
>>
>>   print for @ary;
>
> That's not really an alternative

It was a rewrite of your

    for (0..#$arrayname) { print $array[$_] }

which is at least a bad example of the usage of $#arrayname, because it
doesn't do anything useful with the index. Would you have written
something like

    for ( $[ .. $#arrayname )
    {
        print $_, $array[$_], "\n"
    }

then you would have used the index for something else than what is
better written with a "foreach" type loop. So it was not an alternative,
it was a correction.

-- 
Affijn, Ruud

"Gewoon is een tijger."



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to