mak.jordan has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, i have a problem to acess data from 3D hash. i don't know where i put my data to hash correctly or have accesssing problem in my code..
open (INFILE1,"timing_manual.rpt"); my %PG; use strict; use warnings; while(<INFILE1>){ if (/Path Group:\s+(\S+)/) { $temp = $1; $temp =~ s/(\*)+//g ; ##--here i read one timing file & from that i extract some stringname +like("PLB","DCR") and put it into $temp..##-- $count_{$temp}++; $PG{$temp_{$cou}} = "$start_brckt$end_brckt"; ## ----here i put some scaler data like (reg2reg,in2reg,reg2out,in2ou +t) for my requirment and put it in to hash of hash.. ##---- } if (/slack\s+\(VIOLATED\)/) { chomp $_ ; $_ =~ s/slack\s\(VIOLATED\) \s+//g ; $_ =~ s/\s+//g; $PG{$temp}{$slack} = $_ ; #--#here from reading file i extract the numeric value(1,0.2,-0.50) an +d put it into $PG hash..##-- } } for my $pathgroup1 ( keys %PG) { for my $path1 ( keys %{$PG{$pathgroup1}}) { for my $path2 ( keys %{$PG{$pathgroup1}{$path1}}) { print "\t$pathgroup1 \t $path1 \t $path2 \t\n"; } } } *********************************************************** TIMING REPORT FROM WHERE I WANT EXTRACT ALL INFO. *********************************************************** Startpoint: PLB4ARB8M_ARB_TOP1 (input port edge-triggered flip-flop clocked by PLBCLK1) Endpoint: PLB4ARB8M_ARB_TOP1/reg_PLB_WrBusControlStates_reg[9] (rising edge-triggered flip-flop clocked by PLBCLK1) Path Group: PLBCLK1 Path Type: max Point Incr P +ath -------------------------------------------------------------------- +------ 0.00 2 +.80 r library setup time -1.54 1 +.26 data required time 1 +.26 -------------------------------------------------------------------- +------ data required time 1 +.26 data arrival time -3 +.24 -------------------------------------------------------------------- +------ slack (VIOLATED) -1 Startpoint: PLB4ARB8M_ARB_TOP1/reg_PLB_pacr_reg[2] (rising edge-triggered flip-flop clocked by PLBCLK1) Endpoint: PLB4ARB8M_ARB_TOP1/reg_PLB_pesr_reg[10] (rising edge-triggered flip-flop clocked by PLBCLK1) Path Group: PLBCLK1 Path Type: max Point Incr P +ath -------------------------------------------------------------------- +------ clock PLBCLK1 (rise edge) 0.00 0 +.00 clock network delay (ideal) 0.00 0 +.00 -------------------------------------------------------------------- +------ data required time 2 +.73 data arrival time -3 +.65 -------------------------------------------------------------------- +------ slack (VIOLATED) -2 Startpoint: PLB4ARB8M_ARB_TOP1/reg_PLB_pacr_reg[2] (rising edge-triggered flip-flop clocked by PLBCLK1) Endpoint: PLB4ARB8M_ARB_TOP1 (output port edge-triggered flip-flop clocked by PLBCLK1) Path Group: PLBCLK Path Type: max Point Incr P +ath -------------------------------------------------------------------- +------ clock PLBCLK1 (rise edge) 0.00 0 +.00 data arrival time -3 +.24 -------------------------------------------------------------------- +------ slack (VIOLATED) -3 Startpoint: CPU_DCRADDR[8] (input port clocked by DCRDATA) Endpoint: PLB_DCRDATA[9] (output port clocked by DCRDATA) Path Group: PLBCLK Path Type: max Point Incr Path ----------------------------------------------------------- clock DCRDATA (rise edge) 0.00 0.00 ----------------------------------------------------------- data required time 1.14 data arrival time -1.46 ----------------------------------------------------------- slack (VIOLATED) -4
what i want output is like :
%PG { PLB { in2reg { -0.50 0.2 } reg2out { -0.50 0.2 } } DCR { in2out { -0.50 0.2 } reg2out { 0.50 0.2 } } }
But its not came like that please help me out.. whats my mistake ?? i put data to hash wrongly or there is printing mistake?? Thanks for our time !

Replies are listed 'Best First'.
Re: Hash of hash??
by tobyink (Canon) on Apr 10, 2014 at 12:16 UTC

    Firstly, the code you've shown isn't going to print anything, let alone the wrong thing. There are a number of variables %count_, $temp, $start_brckt, $end_brckt, %temp_, $cou, and $slack, which have not been declared anywhere, so Perl will kick up a stink at compile-time, and won't even attempt to run your code.

    Secondly, it's not clear how you expect to get from your input data to your expected output. The expected output appears to show the numbers -0.50 and 0.2 a lot, but the input data doesn't include them, and you aren't doing any maths in your code except for a ++ which usually implies integer maths. (Though actually in Perl, ++ works on floating point numbers, and strangely, on strings.)

    Lastly, you don't seem to have a clear idea of how to go about solving your problem. It looks like you've adopted the approach of diving in and typing stuff and hoping that it works. When it hasn't you've added a few extra braces and dollar signs, crossed your fingers and prayed to whatever deity you believe in!

    Step back from the problem. Print out a hard copy of your sample input report on real, actual paper. Get two rulers (no, not these ones), a pencil, and a blank sheet of paper.

    Arrange the rulers on the print-out so that you can see a single line of text between them. This is your while loop. It will allow you to step through the file one line at a time without looking ahead or back.

    The blank paper and the pencil are your %PG hash.

    You are Perl.

    While Perl (i.e. you) goes though the input file (i.e. the print-out), it should generate the data structure you want (i.e. you should draw on the blank paper). Think about what features of each line cause what changes to the data structure.

    When you've completed that exercise, you should have a much better idea of the algorithm you need to use to generate your data structure. Read perllol and Mini-Tutorial: Dereferencing Syntax to refresh your memory on how nested data structures work in Perl.

    Then sit down and code.

    In the mean time, here's a nice 3D hash.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Hash of hash??
by toolic (Bishop) on Apr 10, 2014 at 16:48 UTC