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

I am reading an input file to create a hash. My data looks like this. This is after I ran the code without sorting by the value. I need to add the sort by value,ie delay.

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

to create the hash I read in a file

    $timing1{$related_pin}{$timing_type}{$rise_fall}{$pinname} = $delay;

Here is my code

foreach my $rName ( keys %timing1 ) { foreach my $tType ( sort keys %{$timing1{$rName}}) { + foreach my $rF ( sort keys %{$timing1{$rName}{$tType}}){ + foreach my $pinName ( keys %{$timing1{$rName}{$tType}{$rF}}) +{ print OUTFILE2 " $pinName\t $rName\t $tType\t $rF\t $timin +g1{$rName}{$tType}{$rF}{$pinName} \n"; } } } }

Replies are listed 'Best First'.
Re^3: Sorting Multilevel Hashes
by NetWallah (Canon) on Apr 25, 2014 at 01:09 UTC
    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

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

      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

Re^3: Sorting Multilevel Hashes
by Anonymous Monk on Apr 24, 2014 at 23:48 UTC