D
David Filmer
Suppose I have an array of hashes of names and social-security
numbers:
@ARRAY = (
{ first => 'John', last => 'Doe', ssn => '123-45-6789' },
{ first => 'Fred', last => 'Ree', ssn => '9876-54-321' }
);
Now suppose I want to print an error if any particular SSNs (say
'234-56-7890' and '0987-65-4321') are not found in my @ARRAY.
Obviously I can't just grep the array. I could build an intermediate
hash (%SSN) like this:
for (@ARRAY) { $SSN{$$_{'ssn'}}++ }
for (qw/234-56-7890 0987-65-4321/) {
print "Not found: $_\n" unless $SSN{$_}
}
but that's REALLY ugly (I don't like the creation of the intermediate
%SSN hash). I would appreciate suggestions for a more elegant
approach.
numbers:
@ARRAY = (
{ first => 'John', last => 'Doe', ssn => '123-45-6789' },
{ first => 'Fred', last => 'Ree', ssn => '9876-54-321' }
);
Now suppose I want to print an error if any particular SSNs (say
'234-56-7890' and '0987-65-4321') are not found in my @ARRAY.
Obviously I can't just grep the array. I could build an intermediate
hash (%SSN) like this:
for (@ARRAY) { $SSN{$$_{'ssn'}}++ }
for (qw/234-56-7890 0987-65-4321/) {
print "Not found: $_\n" unless $SSN{$_}
}
but that's REALLY ugly (I don't like the creation of the intermediate
%SSN hash). I would appreciate suggestions for a more elegant
approach.