Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: $#array issue

by citromatik (Curate)
on Jun 14, 2009 at 23:01 UTC ( #771481=note: print w/ replies, xml ) Need Help??


in reply to $#array issue

If you comment the second line, $end will be undefined in the loop condition. Please, use strict and use warnings in your code if you want to learn Perl the easy way. This code works as expected:

use strict; use warnings; my @array=(1,2,3,4,5,6,7,8,9); my $end=($#array+1); my $index = 0; while($index < $end){ my $last=pop(@array); print "$last\n"; $index++; }

BTW, here are another ways to address the same problem:

my @array = (1..9); print +(pop @array),"\n" while (@array);

or even

my @array = (1..9); print "array[$_]\n" for (reverse (0..$#array));

citromatik


Comment on Re: $#array issue
Select or Download Code
Replies are listed 'Best First'.
Re^2: $#array issue
by Your Mother (Chancellor) on Jun 14, 2009 at 23:14 UTC
    perl -le 'print and sleep 1 for 1 .. 3, "Jinx, you owe me a Coke."'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://771481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2015-07-31 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (277 votes), past polls