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 $_
    $complex_hash{$key} = $value; 
}

for my $element (@ex) {
    foreach my $key (keys %complex_hash) {
    if($key =~ /$element/) {
            print " $element : \t\t\t ",  $complex_hash{$key}, "\n";
            last;#no more iterations, element found so break from the foreach 
loop
    }
    }       
}

close $fh;
[/code] 

[output]
 5326 :  xng
 2041 :  VG
 1391 :  GYX
 1439 :  IDT
 x1259 :  IDT
[/output]
 
best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


On Friday, 13 December 2013 8:39 PM, Shawn H Corey <shawnhco...@gmail.com> 
wrote:
On Fri, 13 Dec 2013 10:01:13 -0500
Uri Guttman <u...@stemsystems.com> wrote:

> > open my $fh, "<", "fullhost.txt" or die $!;
> > while (<$fh>) {  
> 
> while( my $line = <$fh> ) {
> >      chomp;

    chomp $line;

> >      my $line = $_;  
> 
> no need to copy $_ as the while did it.


-- 
Don't stop where the ink does.
    Shawn


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

--
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