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

I'm writing a script to capture the number of pings sent and received on a per interface basis. The data is available in a text file - with one line for the ping send and another for each ping response. There could be multiple ping sends with successive increased timeouts if there is no ping response.
I'm using a hash to hold the values with the data structure as shown below:
%frq_node = (); # Each hash entry will have a key corresponding to an interface # Each value is an array of 2 elements; element 0 is a hash # with each hash key being the timeout value of a ping send # and the value being the number of pings for that hash # value. element 1 is the number of received pings # open file code here while (<FH>) { if (/sending ping/) { # Extract interface key from line into $key # Extract the timeout value in sending ping $to # Increment the hash value of the corresponding # timeout bucket for the interface ${$frq_node{$key}[0]}{$to}++; # Does not work } elsif (/recevied ping response) { # Increment # of received pings for interface $frq_node{$key}[1]++; # Works fine! } }
Any ideas on where i'm going wrong with "${$frq_node{$key}[0]}{$to}++; " ?
Thanks !

Replies are listed 'Best First'.
Re: Anonymous hash with array
by kesterkester (Hermit) on Sep 19, 2003 at 16:42 UTC
    You don't need the extra {}s around frq_node in the if block... $frq_node{$key}[0]{$to}++ should work.

    In general, I think it's easier to use a reference to an anonymous hash in complicated data structures like these.

    Depending on your data, you might be able to get away with two ifs instead of the if/elsif block.

    Also, you didn't close the elsif pattern match.

    my $frq_node = {}; while (<FH>){ $frq_node->{$key}[0]{$to}++ if /sending ping/; $frq_node->{$key}[1]++ if /recevied ping response/; }
    might work for you.
Re: Anonymous hash with array
by simonm (Vicar) on Sep 19, 2003 at 16:46 UTC
    I'm not sure of exactly why that doesn't work, but I find it's easier to read and write this kind of de-referencing with the arrow notation: $frq_node{$key}->[0]->{$to} ++; that might fix things for you...