Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Lost hash values

by stephen_isa (Initiate)
on May 16, 2005 at 20:10 UTC ( #457544=perlquestion: print w/replies, xml ) Need Help??

stephen_isa has asked for the wisdom of the Perl Monks concerning the following question:

Hi, The following hash prints all the values(total 81) alright on line16(commented) of the code below but prints only 12 values in the final foreach loop. Any clues? Thanks.
#!/usr/bin/perl -w 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 "$first is $save{$first}\n"; } } } foreach $first (sort keys %save){ if ($save{$first}){ print "$first is $save{$first} ms.\n"; } }

Replies are listed 'Best First'.
Re: Lost hash values
by Transient (Hermit) on May 16, 2005 at 20:18 UTC
    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?
      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.
Re: Lost hash values
by tlm (Prior) on May 16, 2005 at 23:37 UTC

    FWIW, your script works fine for me.

    I noticed that your printout does not match what your script is doing. For example, in your code, the print out should have the word "is", but this is not what the output shows; and the code prints a newline after "ms", but this does not show in the output. It would help to see the code that actually produces the output.

    the lowliest monk

      My previous replies were hidden. One has to click on the 2nd "reply4" in this thread and then reply7 to see the fix: BTW, I am new to this posting, how to aviod this? Anyway, here is the final script:
      #!/usr/bin/perl -w open FILE1,"out.prod1" or die "Cannot open file $!"; @rec=<FILE1>; $first=undef; %save=(); foreach $x (@rec){ chomp $x; if ( $x =~ /2005/ ){ $first=$x; if(!defined $save{$first}){ $save{$first}=undef;} } if ( $x =~ /time/ ){ $lat=$x; $lat =~ s/.*?time=(\d+)\.\sms$/$1/; if( $lat > "10" ){ $save{$first}=$lat; } } } foreach $first (sort keys %save){ if ($save{$first}){ print "$first $save{$first}\n"; } }
Re: Lost hash values
by TheStudent (Scribe) on May 16, 2005 at 23:40 UTC
    Some indentation/formatting would be nice.
      Hope the indentation above looks okay.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://457544]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2021-05-06 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (69 votes). Check out past polls.

    Notices?