Hi guys! I am looking to optimize my perl code and would appreciate if you can give some tips on that. I have written a code which reads text from standard input. STDIN consists of elements reading a phone directory first and then has the list of names against which it matches the phone directory.
STDIN form: 4 (represents no. of entries in phone directory) tom 332211 harry 112233 ryan 445566 john 334455 jack john jay Desired Output: Not found john 334455 Not found Actual Code: use strict; use warnings; my @N = <STDIN>; my $x = $N[0]; my @data; my @value; for ( my $j = 1 ; $j <= $x ; $j++ ) { my $s = $N[$j]; chomp $s; $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 my $key; foreach $key ( keys %hash ) { push( @data, $key ); my $value = $hash{$key}; push( @value, $value ); } } my $len1 = $#data; splice @N, 0, $x + 1; chomp @N; my $len2 = $#N; for ( my $i = 0 ; $i <= $len2 ; $i++ ) { if ( grep( /^$N[$i]/, @data ) ) { for ( my $m = 0 ; $m <= $len1 ; $m++ ) { if ( $data[$m] eq $N[$i] ) { print("$data[$m]=$value[$m]\n"); } } } else { print("Not found\n"); } }
In reply to Perl program to look into the phone directory and in case of a match, print the name along with the number by dk27
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |