Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Sorting Multilevel Hashes

by NetWallah (Canon)
on Apr 25, 2014 at 01:09 UTC ( [id://1083702]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Sorting Multilevel Hashes
in thread Sorting Multilevel Hashes

Something like this :
use strict; use warnings; my %timing1; chomp ($_=<DATA>); # Hdr my @hdr = split /\s+/; while (<DATA>){ chomp ; s/^\s+//; my @r=split /\s+/; $timing1{$r[1]}{$r[2]}{$r[3]}{$r[0]}=$r[4]; } foreach my $rName ( keys %timing1 ) { foreach my $tType ( sort keys %{$timing1{$rName}}) { foreach my $rF ( sort keys %{$timing1{$rName}{$tType}}){ + my @by_val = map {[$timing1{$rName}{$tType}{$rF}{$_},$_]} keys %{$timing1{$rName}{$tType}{$rF}}; @by_val = sort { $a->[0] <=> $b->[0]} @by_val; foreach my $aref ( @by_val) { my ($val, $pinName) = @$aref; print " $pinName\t $rName\t $tType\t $rF\t $val \n"; } } } } __DATA__ pin Name related_pin time_type rise_fall delay DQ5_RX_CLK M2CLKP c_rise rise_transition 0.014446 DQ2_RX_CLK M2CLKP c_rise rise_transition 0.014464 DQ0_RX_CLK M2CLKP c_rise rise_transition 0.014452 DQ3_RX_CLK M2CLKP c_rise rise_transition 0.014452 DQ7_RX_CLK M2CLKP c_rise rise_transition 0.014430 DQ4_RX_CLK M2CLKP c_rise rise_transition 0.014446 DQ8_RX_CLK INT_CLK c_fall fall_transition 0.199360 DQ6_RX_CLK INT_CLK c_fall fall_transition 0.199322 DQ1_RX_CLK INT_CLK c_fall fall_transition 0.199500 DQ5_RX_CLK INT_CLK c_fall fall_transition 0.199248 DQ2_RX_CLK INT_CLK c_fall fall_transition 0.199368
OUTPUT:
DQ7_RX_CLK M2CLKP c_rise rise_transition 0.014430 DQ4_RX_CLK M2CLKP c_rise rise_transition 0.014446 DQ5_RX_CLK M2CLKP c_rise rise_transition 0.014446 DQ3_RX_CLK M2CLKP c_rise rise_transition 0.014452 DQ0_RX_CLK M2CLKP c_rise rise_transition 0.014452 DQ2_RX_CLK M2CLKP c_rise rise_transition 0.014464 DQ5_RX_CLK INT_CLK c_fall fall_transition 0.199 +248 DQ6_RX_CLK INT_CLK c_fall fall_transition 0.199 +322 DQ8_RX_CLK INT_CLK c_fall fall_transition 0.199 +360 DQ2_RX_CLK INT_CLK c_fall fall_transition 0.199 +368 DQ1_RX_CLK INT_CLK c_fall fall_transition 0.199 +500

        What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
              -Larry Wall, 1992

Replies are listed 'Best First'.
Re^4: Sorting Multilevel Hashes
by Anonymous Monk on Apr 25, 2014 at 04:04 UTC

    Great. This is exactly what I needed. thanks a ton. Appreciate it

Re^4: Sorting Multilevel Hashes
by Anonymous Monk on Apr 25, 2014 at 18:31 UTC

    This works great though I do not fully understand the value sort . My 4 page program is reduced to 1 page now.One more thing I need from this hash is that I need to be able to access the max and min values in each set . For example mindelay =DQ7_RX_CLK rise_transition 0.014430 maxdelay =DQ2_RX_CLK rise_transition 0.014464 spread = maxdelay -mindelay etc Please help with this too.

      I'm not sure I captured exacttly which min and max you wanted, but this code should get you started on the right track:
      use strict; use warnings; my %timing1; my %minmax; chomp ($_=<DATA>); # Hdr my @hdr = split /\s+/; # Not used while (<DATA>){ chomp ; s/^\s+//; my @r=split /\s+/; $timing1{$r[1]}{$r[2]}{$r[3]}{$r[0]}=$r[4]; $minmax{"$r[3]"}{MIN} ||= 9e9; $r[4] < $minmax{$r[3]}{MIN} and do{$minmax{$r[3]}{MIN} = $r[4]; $min +max{$r[3]}{MINNAME} = $r[0]}; $minmax{$r[3]}{MAX} ||= -9e9; $r[4] > $minmax{$r[3]}{MAX} and do{$minmax{$r[3]}{MAX} = $r[4]; $min +max{$r[3]}{MAXNAME} = $r[0]}; } foreach my $rName ( keys %timing1 ) { foreach my $tType ( sort keys %{$timing1{$rName}}) { foreach my $rF ( sort keys %{$timing1{$rName}{$tType}}){ + my @by_val = map {[$timing1{$rName}{$tType}{$rF}{$_},$_]} + keys %{$timing1{$rName}{$tType}{$rF}}; @by_val = sort { $a->[0] <=> $b->[0]} @by_val; foreach my $aref ( @by_val) { my ($val, $pinName) = @$aref; print " $pinName\t $rName\t $tType\t $rF\t $val \n"; } } } } # Min and Max ... for my $k (sort keys %minmax){ print " $minmax{$k}{MINNAME} $k \t Min=", $minmax{$k}{MIN}, "\t $minmax{$k}{MAXNAME} Max=", ($minmax{$k}{MAX} ) , "\t Spread=", ($minmax{$k}{MAX} - $minmax{$k}{MIN} ) , "\n"; } __DATA__ pin Name related_pin time_type rise_fall delay DQ5_RX_CLK M2CLKP c_rise rise_transition 0.014446 DQ2_RX_CLK M2CLKP c_rise rise_transition 0.014464 DQ0_RX_CLK M2CLKP c_rise rise_transition 0.014452 DQ3_RX_CLK M2CLKP c_rise rise_transition 0.014452 DQ7_RX_CLK M2CLKP c_rise rise_transition 0.014430 DQ4_RX_CLK M2CLKP c_rise rise_transition 0.014446 DQ8_RX_CLK INT_CLK c_fall fall_transition 0.199360 DQ6_RX_CLK INT_CLK c_fall fall_transition 0.199322 DQ1_RX_CLK INT_CLK c_fall fall_transition 0.199500 DQ5_RX_CLK INT_CLK c_fall fall_transition 0.199248 DQ2_RX_CLK INT_CLK c_fall fall_transition 0.199368

              What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                    -Larry Wall, 1992

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found