in reply to Is it ok to assign a popped value to a variable as shown.
my $saved = pop @array; print "Now we have @array and I have saved $saved\n"; $saved = pop @array; print "Now we have @array and I have saved $saved\n";
Personally, I like declaring at the top, especially if a variable is used several times, but others prefer declaring at the point at which the variable is first used. I still haven't figured out which method is really superior.
Here's a few more samples of interesting things you can do with arrays:
Using the standard output variable instead of declaring a new one:
$_ = pop @arr; print "Value popped is $_\n";
Cycling through the array, method 1 (each value is put in standard output variable in turn):
print "Value is $_\n" for @arr;
Cycling through the array, method 2 ($# represents the subscript of the last item in the array):
print "Value is $arr[$_]\n" for 0..$#arr;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is it ok to assign a popped value to a variable as shown.
by GrandFather (Saint) on Dec 06, 2011 at 01:01 UTC |