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

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (3)
    As of 2018-03-19 03:40 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (232 votes). Check out past polls.