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 = ; 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 phonebook 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"); } }