in reply to Re: array with s///g command
in thread array with s///g command
Or in a single statement:my @array2 = @array1; s/\s+//g for (@array2);
The main concern you must have when dealing with map and a regex is not destructing the original array unless you truly intend to. $_ is an alias to each element of the source array, so any modification of it modifies the original array. To avoid this we assign to a temporary lexical in the above map block. And then finally make sure to return the actual variable and not a count of the number of substitutions.my @array2 = map {(my $x = $_) =~ s/\s+//g; $x} @array1;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: array with s///g command
by convenientstore (Pilgrim) on Jul 30, 2007 at 00:36 UTC | |
by GrandFather (Saint) on Jul 30, 2007 at 01:32 UTC | |
by convenientstore (Pilgrim) on Jul 30, 2007 at 06:50 UTC | |
Re^3: array with s///g command
by Anno (Deacon) on Jul 30, 2007 at 09:48 UTC |