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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.