Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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":

  • 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 or How to display code and escape characters are good places to start.
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 perusing the Monastery: (2)
As of 2021-06-22 02:28 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (100 votes). Check out past polls.