in reply to Re: Possible to use an expression as the hash key with exists?
in thread Possible to use an expression as the hash key with exists?
my @full_isbns = ( defined $hashref_isbn ? keys %{$hashref_isbn} : () +); my %isbn_by_stub = (); foreach my $full_isbn ( @full_isbns ) { my $stub_isbn = substr $full_isbn, 6, 5; $isbn_by_stub{$stub_isbn} = $isbn; } # 4-digit ISBN stub if ( $isbn =~ m/^\d{4}$/ ) { $isbn = 0 . $isbn; } # 5-digit ISBN stub if ( $isbn =~ m/^\d{5}$/ ) { if ( exists $isbn_by_stub{$isbn} ) { print "Found! $isbn matches $isbn_by_stub{$isbn}\n"; } }
|
|---|