Steve Bertrand <st...@ibctech.ca> wrote: > Roman Makurin wrote: > > On Wed, Jun 24, 2009 at 03:25:57PM +0200, Jenda Krynicky wrote: > >> From: Roman Makurin <dro...@gmail.com> > >>> here is complite perl script which produces such results without > >>> any warning: > >>> > >>> #!/usr/bin/perl > >>> > >>> use strict; > >>> use warnings; > >>> > >>> use constant { > >>> A => 0, > >>> B => 1, > >>> C => 2 }; > >>> > >>> my @a = (A, B, C); > >>> my @b = (1, 2, 3); > >>> > >>> while(my $i = shift @a) { > >>> print $i, $/ > >>> } > >> But of course this does not print anything. The shift(@a) returns the > >> first element of @a which is zero, assigns that to $i and then checks > >> whether it's true. And of course it's not. So it skips the body and > >> leaves the loop. Keep in mind that the value of > >> > >> my $i = shift @a > >> > >> is NOT a true/false whether there was something shifted from the > >> array. It's the value that was removed from the array and assigned to > >> the $i. And if that value it false (undef, 0, 0.0, "0", "0.0", "" - > >> if I remember rigth) then the whole expression evaluates to false in > >> boolean context. > > If I understand correctly, what you are saying is that while() is > evaluating the left side of the '=' as it's condition, culminating into: > > while($i) > > Which eventually equates into: > > while(0) > > ...on the very first pass.
Well, you could understand it like that in this case. The problem comes as soon as the assignment is not a scalar, but rather a list one. Which doesn't mean just this my @foo = whatever(); but also my ($x, $y) = whatever(); and even my ($x) = whatever(); Because the scalar value of such an assignment is the number of assigned values, not the first or last such value. See print scalar(my $x = 'hello'),"\n"; print scalar(my ($x) = 'hello'),"\n"; print scalar(my ($x,$y) = 'hello'),"\n"; If you want to loop over a list and "consume" it by the loop you should use this: while (@array) { my $x = shift(@array); ... } Jenda ===== je...@krynicky.cz === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/