Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

RE: RE: Re: parsing

by ase (Monk)
on Jul 27, 2000 at 15:34 UTC ( [id://24657]=note: print w/replies, xml ) Need Help??


in reply to RE: Re: parsing
in thread parsing

This should probably be a Q&A but, "I understand your pain" in regards to map. Up until _very_ recently I didn't really understand it either (probably still don't -not like the true monks) but here goes:

Map executes a block of code for each element in a list and returns a list of the results. The magical variable $_ is set (actually localized so it isn't clobbered outside the block) to each element within the block. Map is _extremely_ powerful when in the hands of the likes of merlyn and friends. (i.e. Schwartzian Transform)
A couple (simple) examples to illustrate:
copy a list (i.e. do nothing same as @b = @a;)

@b = map {$_} @a;

Take a list of numbers (a vector) square them, add them together and take the square root (Euclidian N-dimensional distance):

 $dist = sqrt(eval join("+",map {$_**2} @a));

Explanation of that last one: map squares each element in @a (say 1,2,3,4,5) and returns a new list (1,4,9,16,25) then join makes a scalar "1+4+9+16+25", the eval makes it 55 and finally sqrt returns 7.41619... which is assigned to $dist.

For another example see A minor epiphany with grep and map as well as every piece of documentation on the topic you can get your hands on. I think what did it for me (finally) was Effective Perl Programming by Joseph N. Hall with Randal L. Schwartz. The section (Item 12) is only 4 pages long and also covers grep and foreach but, it was the lightswitch for me.
-ase

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://24657]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found