in reply to Re^2: Perl program to look into the phone directory and in case of a match, print the name along with the number
in thread Perl program to look into the phone directory and in case of a match, print the name along with the number
I'm not sure whether the optimization would contribute much to the overall speedup, but the following two lines do much needless work:
$s =~ s/\s+/=/g; my @v = split( /[\[\],]/, $s ); #create an array from string $s # assign hash value pairs and insert '=' between them to create phoneb +ook my %hash = map { split( /=/, $_, $x + 1 ) } @v; entry
Why do you convert all whitespace in $s to = and then split on = again? What use is @v?
Also, the fasted way to do general lookups in Perl is to use a hash. In your case, the fastest data structure to look up the name from a number would be a hash that maps each number to a name:
my %reverse_phonebook = ( 123 => 'jack', 456 => 'jill', ... );
Building this data structure allows for quick repeated lookups of names if all you have is the number.
|
|---|