Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perl style... help me figure this out.

by mcdave (Beadle)
on Feb 07, 2012 at 04:25 UTC ( [id://952208]=note: print w/replies, xml ) Need Help??


in reply to Perl style... help me figure this out.

So, you've got %$hash whose values are arrayrefs, and you've got %$values whose keys are things you're looking for, and you want to find those keys $k in %$hash such that @{$hash->{$k}} contains a key from %$values. Is that the problem you're trying to solve?

Assuming it is, then it seems grep and array are pretty much what you're after (try as you might to avoid them), since you're asking for $k such that grep { $values->{$_} } @{$hash->{$k}} is nonempty.

OK, this isn't going to work that well for large lists, because grep can't short-circuit and you'll waste time after you "already" know the answer is yes. Maybe wait for Perl 6? No, probably not a good idea. So you'll need to do your own short-circuiting by iterating over @{$hash->{$k}} and quitting when you've found something that matches.

Since nothing is sorted anywhere in this exercise, I don't think you'll do much better than "iterate over the keys of %$hash, then iterate over the entries in the arrayref and check each one".

Replies are listed 'Best First'.
Re^2: Perl style... help me figure this out.
by tobyink (Canon) on Feb 07, 2012 at 07:42 UTC

    If you want grep to short circuit, try first from List::Util.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://952208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found