Oh, come on now. That would never work. If $red was true, you'd get all your tshirts, regardless of colour, while, if false, you'd be overdressed at the beach.

What I think you meant was: grep { $_->{colour} eq 'red' } @tshirts or, if you're an OO type of guy, grep { $_->colour() eq 'red' } @tshirts.

However, even that can get a bit messy. I mean, some of us have two, three, or even more, red shirts. Putting them all on at once would look silly, even for a computer geek. (Though, if you're in a workplace filled with perl geeks, you could do that one day - show up wearing all your red shirts, and if anyone asks why you're wearing so many shirts, just tell 'em that you did a grep { $_->has_colour('red') } @tshirts on your closet that morning...). Instead, what you really want is either use List::Utils qw/first/; first { $_->colour() eq 'red' } @tshirts or possibly my @reds = grep { $_->colour() eq 'red' }; $reds[rand @reds] depending on whether you want to be efficient or you prefer some variety.

Me? Too much perl? Nah ...


In reply to Re^2: Too Much Perl? by Tanktalus
in thread Too Much Perl? by apotheon

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!
  • 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, details, 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, summary, 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.