davies has asked for the wisdom of the Perl Monks concerning the following question:
The following code does what I want:
use strict; use warnings; use diagnostics; my @list = (["qwe", 1], ["qwer", 2], ["tyui", 3], ["asdf", 4], ["ghjk" +, 5], ["zxcv", 6] ); my @list2 = map $$_[0], @list; for my $item ("qwe", "qwer", "tyu", "yui", "tyui") { print grep(/^$item$/, @list2) . " " . $item . "\n"; }
I would prefer to avoid creating @list2 every time (or an anonymous array/list via some other use of map), but I'm stuck. My instincts tell me that it should be possible, but trial results in nothing but error and Google gives me nothing I find helpful. Is there a way of using grep directly on an AoA?
Regards,
John Davies
Update: Thank you both for working solutions that through their slight differences have taught me more than I was expecting.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Grep with AoA
by grizzley (Chaplain) on Aug 27, 2012 at 09:43 UTC | |
|
Re: Grep with AoA
by moritz (Cardinal) on Aug 27, 2012 at 09:43 UTC |