in reply to Re: Putting Hash values into an array
in thread Putting Hash values into an array

Hello Athanasius,

I was going to say the same thing and then I did the 1 liner:
perl -E 'use strict; foreach (my $RID) { say "okay"; }' --------------------------------- Output: 'okay'

I changed the 'foreach' to 'while' and it didn't enter the loop. Some monk more familiar with the internals of Perl, will have to clarify. I'm guessing that 'while' tests at the beginning of the loop, and 'foreach' tests at the end of the loop. But that is just a guess.

I did the test to see if 'use strict;' would pick up the error. But if didn't and the script worked, so I kept my mouth shut and looked for someone to give a better answer, which you did(++).

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

Replies are listed 'Best First'.
Re^3: Putting Hash values into an array
by Athanasius (Archbishop) on Feb 28, 2014 at 08:52 UTC

    You’re right! But at least warnings raises a red flag:

    18:38 >perl -Mstrict -wE "foreach (my $RID) { say qq[\$RID = >$RID<]; +}" Use of uninitialized value $RID in concatenation (.) or string at -e l +ine 1. $RID = >< 18:43 >

    I wasn’t expecting the loop to be entered at all, and Foreach Loops doesn’t seem to shed much light on the issue. But I’m guessing it’s the difference between a scalar undef and an empty list:

    18:45 >perl -Mstrict -wE "foreach (my @RID) { say 'okay'; }" 18:46 >

    Seems the foreach is treating $RID as a list containing one element (with the value undef), as though it were foreach (my @RID = (undef)):.

    18:46 >perl -Mstrict -wE "foreach (my @RID = (undef)) { say 'okay'; }" okay 18:51 >

    Thanks for pointing this out!

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,