in reply to Nested loops?
If I'm understanding you correctly, exists to the rescue!
use warnings; use strict; my %one = (a => 1, b => 2, c => 3); # first hash my %two = (a => 1, c => 3); # second hash for my $x (keys %two){ if (exists $one{$x}){ print "hash \$two key $x exists in hash \$one\n"; } }
Output:
hash $two key c exists in hash $one hash $two key a exists in hash $one
In other words, it iterates over the second hash, checking if the "filter" key is in the first hash allowing you to perform some actions, otherwise the loop will just skip to the next iteration. A major benefit here is that it only iterates over a single hash... the smallest one, which completely avoids looping over one hash in its entirety, then a second entire hash for every key in the first.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Nested loops?
by Speed_Freak (Sexton) on Aug 17, 2017 at 21:05 UTC |