in reply to Lost hash values

open FILE1,"out.prod" or die "Cannot open file $!"; @rec=<FILE1>; %save=(); foreach $x (@rec) { if ( $x =~ /2005/ ) { chomp($first=$x); $save{$first}=undef; } if ( $x =~ /time/ ) { chomp($lat= $x); $lat =~ s/.*?time=(\d+)\.\sms$/$1/; if( $lat > "10" ) { $save{$first}=$lat; #print "Hash value is $save{$first}\n"; } } } foreach $first (sort keys %save) { if ($save{$first}) { print "hash value is $save{$first} ms.\n"; } }
If $x !~ /2005/ you have no $first.... thus $first is undef, meaning during the loop it prints out fine, but inside the hash, you're overwriting the same key everytime.

Also, why not just chomp $x at the start and get it over with there instead of chomping twice?

Replies are listed 'Best First'.
Re^2: Lost hash values
by stephen_isa (Initiate) on May 16, 2005 at 20:54 UTC
    Here is the snippet of the input file. If x!~/2005, i want to do nothing. Also if x=~/time/, i want to store values in the key registered with match x=~/2005/ in the previous line(of the input file). The value of the $first key is being printed from the same hash which i use to print from at the end. Thanks for the chomping suggestion.
    ------------------------------------------ ------------------------------------------ Thu May 16 13:15:11 EDT 2005 72 bytes from myhost.xx.yyy.com (168.112.50.50): icmp_seq=0. time=0. m +s ----myhost.xx.yyy.com PING Statistics---- 1 packets transmitted, 1 packets received, 0% packet loss round-trip (ms) min/avg/max = 0/0/0 ------------------------------------------ ------------------------------------------
A reply falls below the community's threshold of quality. You may see it by logging in.