Re: PERL: while () on @Array?

by "John Murray" <jmnc(at)lis.net.au>

 Date:  Wed, 20 Sep 2000 22:59:46 +1000
 To:  <hwg-techniques(at)mail.hwg.org>
  todo: View Thread, Original
"while (@arrayname) {}"

This will do nothing and continue to do nothing - continually!

Run these individual scripts.
#####
@youtherebaby = ("1","2");
if (@youtherebaby) {print "yes\n";}
    else {print "no\n";}
######
@youtherebaby = ();
if (@youtherebaby) {print "yes\n";}
    else {print "no\n";}
######
if (@youtherebaby) {print "yes\n";}
    else {print "no\n";}
#####
@youtherebaby = ("1","2");
while(@youtherebaby) {print "yes\n";}
print "no\n";
####
@youtherebaby = ();
while(@youtherebaby) {print "yes\n";}
print "no\n";
#####
while(@youtherebaby) {print "yes\n";}
print "no\n";


----- Original Message -----
From: Alberto.�.Vallini <vallinis(at)yahoo.com>
To: <hwg-techniques(at)mail.hwg.org>
Sent: Wednesday, September 20, 2000 9:12 AM
Subject: PERL: while () on @Array?


Hallo, in reference with my previous question:

QUOTE:

>5) Last but not least: many a Perl book refer to examples of while loops
>applied to an Array by providing conditions such as: while (@anArray) ;
>the assumption is the cycle will stop with the last element of the array.
>Now either I'm quite dumb or there must be something wrong: any time I
>attempt such a looping, I enter an infinite loop. None the less books keep
>saying it is feasable, right that way. I can quote by Laura Lemay (whose
>Perl book is fairly good, despite I would not reccomend her books on Html
>and surely not her book about javascript):
>"You can ... supply an array variable anywhere a scalar value is expected-
>for example in any of these expressions: ... while (@arrayname) { }."
>Laura Lemay "teach yourself Perl in 21 days" pag 84.
>Useless to say I never thought a programming language can be acquired in
>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=("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
script, it still enters an infinite loop (ie prints nothing). *Also*
putting some placeholder within the loop curly brackets so to make it
perform something (in javascript a conditional expression without block
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
example, and the reply above would confirm it, but I can assure I only get
infinite loops.
Of course Tylers' reply to use sort of while($x<=$#arrayname) and updating
the $x would work, and looked to me too as the sensible thing: what I'm
trying to work out is whether it is true or false that a loop like
while(@anArrayName) would end itself in Perl at the $#arrayname index also
without invoking it directly. My answer would be a downright: no. But since
many books say so and also some of You say so, I'm really wondering...
maybe there must be some "special" conditions but I cannot figure out what
ones...
I'm trying to learn Perl and I must say that these things (*unexpected*
behaviors) are just what makes programming languages hard, no matter what
your background: seems any language exhibits its own!

TIA to you all!



Alberto.�.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