On 12/13/2013 10:21 AM, Shaji Kalidasan wrote:
Greetings,

Thanks a bunch Uri and Shawn for providing valuable inputs.

Here is the optimized code after corrections (based on inputs from Uri and 
Shawn)

[code]
use strict;
use warnings;

my %complex_hash;

my @ex = qw / 5326 2041 1391 1439  x1259 /;

open my $fh, "<", "fullhost.txt" or die $!;

while (<$fh>) {
     chomp;
     my ($key, $value) = split /\s+/; #split on whitespace from $_

you are still using $_ there. it is best to use a lexical scalar variable when possible. it has a name which is good for the reader and it can't be modified elsewhere (as $_ can be).

     $complex_hash{$key} = $value;
}

for my $element (@ex) {
     foreach my $key (keys %complex_hash) {
     if($key =~ /$element/) {

just to make sure, are you checking if the key contains $element or do you want an exact match?

             print " $element : \t\t\t ",  $complex_hash{$key}, "\n";
             last;#no more iterations, element found so break from the foreach 
loop
     }
     }
}

speaking of names, complex_hash is a very poor one. it isn't complex at all as it is a simple one level hash. it says nothing about the content of the hash nor how it is used. picking good names is a major coding skill you (and all beginners) need to do well. it is one of the most important aspects of good coding and it is not emphasized enough.

uri


--
Uri Guttman - The Perl Hunter
The Best Perl Jobs, The Best Perl Hackers
http://PerlHunter.com

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to