Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I thought I'd share an enlightenment I had recently with the community, hoping someone else can have a similar breakthrough. Until now, I've always shied away from map and grep. I understood how they worked (on the surface) but never seemed to find a situation where they "fit". I didn't fully understand their power. It was like having a tool in my toolbox without taking it out of the wrapper.

The other day I was playing around with a two little OO modules, I'll call one Item and one Collection for sake of argument.

Item has two main attributes:
VAL :a scalar value. and
USABLE :a flag that denotes the items availability.

Collection is just that: a collection of Item objects with added methods to do stuff with the collections. It's main attribute is ITEMS which is a referance to an array of Item objects.

Now that I've set the scene and bored half the audience to tears I come to the problem that led to enlightenment. I wanted a method in Collection to return a scalar that was a concatenation of the values (sorted numerically) of all the objects in the collection whose USABLE flag was true. I ended up with this:

#method in Collection object stringify { my $self = shift; return join("",sort {$a <=> $b} map {$_->{VAL}} grep {$_->{USABLE}} @$self->{ITEMS}); }

The great part is, once I wrote it, it seemed 'natural'. It just fit. I used to read this kind of code and go "uhhhh, Ok that's cool but I'll never write something like that let alone be able to decipher it". I feel I've crossed some sort of learning plateau, or passed an important rite of passage.
That's it.. on to the next level

In reply to A minor epiphany with grep and map by ase

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2020-12-01 05:17 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found