in reply to pop an array

If you need to reverse the contents of the array and you don't want to use the reverse function, you could do something like this:

use strict; use warnings; my @foo = @ARGV; # get the array we're reversing # reverse it: my @tempfoo; while (scalar (@foo)) { my $element = pop (@foo); push @tempfoo, $element; } @foo = @tempfoo; # foo has been reversed...

There are probably a hundred different ways to do it if you want to do it just to do it. But stick with reverse unless you absolutely don't want to. It's much cleaner and easier to understand.


Want to support the EFF and FSF buy buying cool stuff? Click here.

Replies are listed 'Best First'.
Re: Re: pop an array
by Fletch (Bishop) on Feb 09, 2004 at 14:40 UTC

    Strictly speaking the scalar is unnecessary. An array in a boolean context is true if it has elements and false if it's an empty list.

    $ perl -le '@a = (); @b = (1); print "\@a" if @a; print "\@b" if @b;' @b
      Strictly speaking it isn't necessary. I usually add it for clarity. TMTOWTDI.

      Want to support the EFF and FSF buy buying cool stuff? Click here.
      A reply falls below the community's threshold of quality. You may see it by logging in.
      (This is off-topic, but I never use scalar with @rrays. I use 0+@array. Indeed, most often uses of scalar can be changed to the shorter 0 + something or "" . something)