That would work except what I'm searching for is a group of numbers but there will be a few sets of numbers in one value. Is there a way to iterate over each key/value in the hash, split them on :: then test existance?
Thanks. | [reply] |
my %matches = map {$_ => 1} 17, 83, 114, 205, 407;
OUTER:
while (my ($key, $value) = each %hash) {
foreach my $num (split /::/ => $value) {
if ($matches {$value}) {
print "$key: $value\n";
last OUTER;
}
}
}
Abigail | [reply] [d/l] |
I've been doing Perl for about 5 years, and I'm *NO* expert, but I have to say that to me that is at least somewhat obfuscated code. I have no problem with the while, but the foreach:
foreach my $num (split /::/ => $value) {
mystifies me - what is the split splitting? When split isn't given an arg to split, it splits $_, but what is $_ in this case, since the while loop gets $key and $value?
IMHO, more straightforward code, albeit sometimes longer code, is better:
my %matches = map {$_ => 1} 17, 83, 114, 205, 407;
while (my ($key, $value) = each %hash) {
my @value_tokens = split /::/, $value;
foreach my $value_token (@value_tokens) {
if ($matches{$value_token}) {
print "$key: $value\n";
$found = 1;
last; ### break out of foreach
}
} ### end foreach
if $found:
next;
}
} ### end while
I'm not entirely sure I understand the original intent, so this solution may need some work.
HTH. | [reply] [d/l] [select] |