On 02/12/2011 11:20, timothy adigun wrote:
Hi Sunita,
On Fri, Dec 02, 2011 at 04:39:20AM -0500, sunita.prad...@emc.com wrote:
Hi All
My array @sympd_list has following lines :
--------------------
/dev/sdd 0BE0 07F:0 08C:D0 Unprotected N/Grp'd RW
500
/dev/sde 0BE1 07F:0 07A:C0 Unprotected N/Grp'd RW
500
/dev/sdf 0BE2 07F:0 08D:C0 Unprotected N/Grp'd RW
500
/dev/sdg 0BE3 07F:0 07B:D0 Unprotected N/Grp'd RW
500
/dev/sdj 201A 07H:0 07A:D5 Unprotected N/Grp'd RW
3
---------------------------
I want second column in another array :
0BE0
0BE1
0BE2
0BE3
201A
I used map function with pattern matching as>>> my @sympd_dev_list =
map {$_ =~ s/^\/\S+\s+([0-9A-F]{4}).+/$1/ } @sympd_list;
I do not get correct output in map function . But if I use same pattern
matching in a foreach loop , then I get right output .
Could anyone tell me what is wrong in my map function ?
There is nothing wrong with your map function, you only forgot the tell
what was matched like this [ Please not * ^^^^ * addition to your code] :
my @sympd_dev_list = map {$_ =~ s/^\/\S+\s+([0-9A-F]{4}).+/$1/;$_ }
@sympd_list;
^^^^
print $_ for @sympd_dev_list; # added just to display
@sympd_dev_list
OUTPUT
========
0BE0
0BE1
0BE2
0BE3
201A
It is NEVER correct to modify $_ within a call to map(), which 'maps'
one list to another and shouldn't be used for its side-effects. If you
think you need to do that then you should be using a for loop.
Few people would think of using any other function for its side-effects.
How much sense does this make?
for my $n (1 .. 10) {
rand(my $n2 = $n * $n);
print $n, ' ', $n2, "\n";
}
Yet using s/// within a map() block does something very similar.
The correct form of a call to map() looks like this
my @sympd_dev_list = map /([0-9A-F]{4})/, @sympd_list;
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/