in reply to Re: Too Much Perl?
in thread Too Much Perl?
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 ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Too Much Perl?
by jdporter (Paladin) on Apr 27, 2006 at 17:55 UTC | |
by GrandFather (Saint) on Apr 27, 2006 at 18:12 UTC | |
by apotheon (Deacon) on Apr 27, 2006 at 20:40 UTC | |
|
Re^3: Too Much Perl?
by Anonymous Monk on Apr 27, 2006 at 17:48 UTC | |
by apotheon (Deacon) on Apr 27, 2006 at 20:39 UTC |