Re: PERL: while () on @Array?
by "ihug" <c-newton(at)ihug.co.nz>
|
Date: |
Wed, 20 Sep 2000 22:47:03 +1200 |
To: |
<hwg-techniques(at)mail.hwg.org> |
References: |
0 idirect |
|
todo: View
Thread,
Original
|
|
Just adding to Allens response.
@arrayname
returns the number of elements in the array
so that so long as this number is > 0 the loop
will continue and as allen say's unless something
is done to reduce the number of elements
in the array towards 0 the loop will continue forever.
Hope this helps.
nb this is a good way to find the number of elements in an array.
for example :-
$number_of_entries = @arrayname;
(remember that it starts at no 0 though
Hope this helps
Peter Newton
"Alberto.�.Vallini" wrote:
> 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/
Alberto,
>From page 121 of "Perl 5 for Dummies" (my perl 'bible'), "Perl terminates
'while' loops whenever one of the following things occurs: 1) Something
inside
the loop makes it so the logical test will change from true to false, and 2)
A
'last' statement in the loop is executed.".
The 'while' statement does not process the list, but merely evaluates it for
true or false. It returns true if the list is not empty and false if it is.
So, changing the 'while' statement above to 'while (@arrayname) {
pop(@arrayname); }' will make it work. Any loop like this has to perform
some
work upon the list and will therefore have to use either 'pop' or 'unshift'
to
remove entries from the list. Once the list is empty, the 'while' logic
returns false and program control moves to the next statement.
HTH
... Al Hubble
--
**************************************************
Allen Hubble [AHubble <AT> idirect.com]
Hubble Technology Consulting, Inc.
EDI/EC, Mfg/Pro, Progress/UNIX, Perl/CGI
Brampton, Ontario, Canada
********** Fortune favours the BOLD! ***********
--
**************************************************
Allen Hubble <AHubble(at)idirect.com>
Hubble Technology Consulting, Inc.
EDI/EC, Mfg/Pro, Progress/UNIX, Perl/CGI
Brampton, Ontario, Canada
********** Fortune favours the BOLD! ***********
HWG hwg-techniques mailing list archives,
maintained by Webmasters @ IWA