Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

While understanding what map{} is supposed to do, I seem to be consistently unable to use it correctly... which suggests that, on some level I really don't know what it actually does.

As I understand it, map{} applies some function (defined within the BLOCK) to each element of an array and the result is passed to the left.

So, you could, for instance, write a for loop without using a for simply by writing:

my @array = (1, 2, 3, 4, 5); my @adjusted_array = map { $_ * 10 } @array; print (join ',', @adjusted_array);

But what about more complicated statements?

I make it about as far as loading a hash from an array that I've split before it all starts to look like a monkey hitting the keys largely at random.

my @array = ('one-1', 'two-2', 'three-3', 'four-4', 'five-5'); my %hash = map { split /-/ } @array; print (join ',', keys %hash);

Why, for instance, could I not work this through even though it's clearly a mapping situation?

if ($access) { foreach (@{$access}) { $set{$_->[0]} = $_->[1]; } }

I guess what I'm seeking is the wisdom to understand how map operates within the BLOCK since most mentions of the map{} function are either cryptic or basic and I fall somewhere in between.

TIA


In reply to Back to Remedial Perl for Me: map{} function by jreades

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-03-29 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found