in reply to Global array afftected by map inside of a subroutine

Are you using Perl 5.14? If so, what happens when you use the non-destructive replacement flag /r on the regex?

Remember that because map operates on $_, it can update array elements in place. Remember also that map returns the return value of its last expression, which is not always $_.


Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^2: Global array afftected by map inside of a subroutine
by Lady_Aleena (Priest) on Dec 13, 2011 at 22:40 UTC

    I'm using Strawberry Perl 5.10 for Windows XP.

    Have a cookie and a very nice day!
    Lady Aleena

      Then you need something like:

      my @short_sizes = map { my $item = $_; $item =~ s/^(\w)\w{1,}$/$1b/; $item } @in_sub_filesize_names;

      Improve your skills with Modern Perl: the free book.

      In map, grep, and in "for" loops, $_ is an alias to the current item, so this would work also:
      my @short_sizes = @in_sub_filesize_names; s/^(\w)\w{1,}$/$1b/ for @short_sizes;