Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm puzzling over the use of map with user-defined functions. Here's some sample code:
sub my_uc { my $c = shift; return uc($c); } my @a = ('a'..'c'); @a = map { uc($_) } @a; print "==>@a<== { uc(\$_) }\n"; @a = map { my_uc($_) } @a; print "==>@a<== { my_uc(\$_) }\n"; @a = map uc, @a; print "==>@a<== uc,\n"; @a = map my_uc, @a; print "==>@a<== my_uc,\n";
The output is:
==>A B C<== { uc($_) } ==>A B C<== { my_uc($_) } ==>A B C<== uc, ==> <== my_uc,
Apparently Perl doesn't support the syntax map function, array for user-defined functions. Or perhaps with this syntax it expects to find the function's argument in $_. So here's my second try:
sub my_new_uc { my $c = shift // $_; return uc($c); } sub my_uc { my $c = shift; return uc($c); } my @a = ('a'..'c'); @a = map my_new_uc, @a; print "==>@a<== my_new_uc,\n"; @a = map my_uc, @a; print "==>@a<== my_uc,\n"; @a = map my_new_uc, @a; print "==>@a<== my_new_uc,\n";
Surprisingly, my new function sometimes works, and sometimes not. Here's the output from that second code fragment:
==>A B C<== my_new_uc, ==> <== my_uc, ==> <== my_new_uc,
The information on map in perlfunc doesn't say anything about using your own functions except in the form map { function($_) } @array and I couldn't find any help in other tutorials or FAQs. Does anyone know what is happening in my second example, and why $_ sometimes is used in map and sometimes not?

In reply to Problems with map(function, array) by Redei

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (14)
    As of 2014-09-22 14:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (196 votes), past polls