One run of the program looks like:#!/usr/bin/perl use warnings; use strict; my @isbn = (); my %hash = (); for ( 0 .. 100) { $isbn[$_] = &gen_isbn_13; } push @isbn, "9780523456789"; push @isbn, "9780523412345"; while(my $pattern = <DATA>) { chomp($pattern); print "Testing pattern = $pattern\n"; $hash{$pattern} = &isbn_exists( $pattern, \@isbn ); print "Matches found! ISBN = $_ \n" for ( @{$hash{$pattern}} ); } sub isbn_exists { my $pattern = shift; my $isbn_arrayref = shift; my @answer = grep { /\Q$pattern\E$/ } @{ $isbn_arrayref }; return \@answer; } sub gen_isbn_13 { my $num = "978"; for ( 1 .. 10 ) { my $digit = int(rand(10)); $num .= $digit; } return $num } __DATA__ 12345 97833 97810 97805
Update: More exact match to original patternC:\Code>perl isbncheck.pl Testing pattern = 12345 Matches found! ISBN = 9780523412345 Testing pattern = 97833 Testing pattern = 97810 Testing pattern = 97805
In reply to Re: Possible to use an expression as the hash key with exists?
by dwm042
in thread Possible to use an expression as the hash key with exists?
by wenD
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |