PERL: while () on @Array?

by =?iso-8859-1?Q?=22Alberto=2E=B7=2EVallini=22?= <vallinis(at)yahoo.com>

 Date:  Wed, 20 Sep 2000 01:12:34 +0200
 To:  hwg-techniques(at)mail.hwg.org
 In-Reply-To:  0
  todo: View Thread, Original
Hallo, in reference with my previous question:

QUOTE:

>5) Last but not least: many a Perl book refer to examples of while loops=20
>applied to an Array by providing conditions such as: while (@anArray) ;=20
>the assumption is the cycle will stop with the last element of the array.=
=20
>Now either I'm quite dumb or there must be something wrong: any time I=20
>attempt such a looping, I enter an infinite loop. None the less books keep=
=20
>saying it is feasable, right that way. I can quote by Laura Lemay (whose=20
>Perl book is fairly good, despite I would not reccomend her books on Html=
=20
>and surely not her book about javascript):
>"You can ... supply an array variable anywhere a scalar value is expected-=
=20
>for example in any of these expressions: ... while (@arrayname) { }."=20
>Laura Lemay "teach yourself Perl in 21 days" pag 84.
>Useless to say I never thought a programming language can be acquired in=20
>21 days, in fact here I am!

UNQUOTE/

I've been told that the following code would work (ie, prints at the end):
QUOTE:

this will end by itself and print "Hello World":
#!/usr/bin/perl

@arrayname=3D("1","2","3","4","5");

while (@arrayname) {}

print "content-type: text/html\n\n";
print "Hello, World...\n";
UNQUOTE/

I tried it on my machine (win2000) at the prompt command; but running the=20
script, it still enters an infinite loop (ie prints nothing). *Also*=20
putting some placeholder within the loop curly brackets so to make it=20
perform something (in javascript a conditional expression without block=20
instructions causes errors), the outcome is the same: an infinite loop=
 again.
I am puzzled since many books bring a while(@array) loop like a working=20
example, and the reply above would confirm it, but I can assure I only get=
=20
infinite loops.
Of course Tylers' reply to use sort of while($x<=3D$#arrayname) and updating=
=20
the $x would work, and looked to me too as the sensible thing: what I'm=20
trying to work out is whether it is true or false that a loop like=20
while(@anArrayName) would end itself in Perl at the $#arrayname index also=
=20
without invoking it directly. My answer would be a downright: no. But since=
=20
many books say so and also some of You say so, I'm really wondering...=20
maybe there must be some "special" conditions but I cannot figure out what=
=20
ones...
I'm trying to learn Perl and I must say that these things (*unexpected*=20
behaviors) are just what makes programming languages hard, no matter what=20
your background: seems any language exhibits its own!

TIA to you all!



Alberto.=B7.Vallini  - vallinis(at)yahoo.com
http://www.geocities.com/earthalliances/
"when everything else fails, sleep"


_________________________________________________________
Do You Yahoo!?
Get your free (at)yahoo.com address at http://mail.yahoo.com

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA