Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me understand this map statement. 1

Status
Not open for further replies.

Enpassant

IS-IT--Management
Sep 24, 2008
2
US
Can someone help me understand the following map statement ?

map { /er(\d+)/o; [$_, $1] } @array;

Contents of the @array: "emcpower0 emcpower10 emcpower5"

Thanks much !
 
The regexp is looking for array elements that have "er" followed by one or more digits, so none of the elements you posted would match. It saves the original array element and the captured group from the regexp (\d+) (but there is none in your example) and saves them in an anonymous array [$_, $1] but since the map function is not returning any data to a new list there is no way to access those anonymous arrays later in the program. It needs to be something like:

Code:
@new_array = map { /er(\d+)/o; [$_, $1] } @array;

which would create an array of arrays.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry, the regexp will match the er0 and er10 and er5 parts of the array elements and store the digits in the anonymous arrays.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
This is probably obvious to others with more map experience, but this had me scratching my head for a while. I had to do some printing to see that KevinADC's @new_array will be an array of array references. To see that, try:

Code:
print @new_array; # ARRAY(0x8a8e034)ARRAY(0x8a8e364)ARRAY(0x8a8e6f4)
print "\n";
print "@{$new_array[0]}\n"; # emcpower0 0
print ${$new_array[0]}[0] . ',' . ${$new_array[0]}[1] . "\n"; # emcpower0,0

Thanks for the good explanation KevinADC.

--
 
You're welcome.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks to both. Now that I understand it, doesn't sound so complex :).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top