subhankar has asked for the wisdom of the Perl Monks concerning the following question:
There's an awk script, which works but I have been told to use Perl to rewrite the same as we need Perl's formatting capabilities. I converted the AWK script to Perl using a2p. The code :
There is a pipe separated file from which certain fields are to be extracted and based on the occurence of a particular type of field, the count has to be gathered. I have cleaned up the initial part of the code where the lines are splitted and the values extracted. Currently I am unable to figure out the implementation of the 3 dimensional hash as in :#!/usr/perl5/5.6.1/bin/perl $[ = 1; # set array base to 1 $FS = '|'; ##SUBSEP = "|"; while (<>) { ($Fld1,$Fld2,$Fld3,$Fld4,$Fld5,$Fld6,$Fld7,$Fld8,$Fld9,$Fld10,$Fld +11,$Fld12,$Fld13,$Fld14,$Fld15,$Fld16,$Fld17,$Fld18,$Fld19,$Fld20,$Fl +d21,$Fld22,$Fld23, $Fld24,$Fld25,$Fld26,$Fld27,$Fld28,$Fld29,$Fld30,$Fld31,$Fld32,$Fld33, +$Fld34,$Fld35,$Fld36,$Fld37,$Fld38,$Fld39,$Fld40,$Fld41,$Fld42,$Fld43 +,$Fld44,$Fld45,$Fl d46,$Fld47,$Fld48,$Fld49,$Fld50,$Fld51,$Fld52,$Fld53,$Fld54,$Fld55,$Fl +d56,$Fld57,$Fld58,$Fld59,$Fld60,$Fld61,$Fld62,$Fld63,$Fld64,$Fld65,$F +ld66,$Fld67) = split(/[|\n]/, $_, 9999); if (!($Fld1 eq '800' || $Fld1 eq '801')) { $CNT{substr($Fld67, 3, 5), $Fld1, $Fld15}++; $COST{substr($Fld67, 3, 5), $Fld1, $Fld15} += $Fld14; } } foreach $l_strSwitch (keys %CNT) { @l_strParams = split($;, $l_strSwitch, 9999); printf "\n%s|%s|%s|%d", $l_strParams[1], $l_strParams[2], $l_strParam +s[3], $CNT{$l_strSwitch}; }
It would be very helpful to me if someone could explain in simple terms, how is this auto-increment and the addition getting done. The foreach loop gets the values from the %CNT, but I also need the corresponding %COST value in the same line.$CNT{substr($Fld67, 3, 5), $Fld1, $Fld15}++; $COST{substr($Fld67, 3, 5), $Fld1, $Fld15} += $Fld14;
Although I am able to get the values, any help in understanding the hash mechanism implemented here, would be very helpful.
Thanks & regards,
Subhankar
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multidimensional Hash implementation/usage
by moritz (Cardinal) on Sep 17, 2007 at 06:32 UTC | |
by salva (Canon) on Sep 17, 2007 at 08:44 UTC | |
by subhankar (Novice) on Sep 17, 2007 at 06:57 UTC | |
by moritz (Cardinal) on Sep 17, 2007 at 07:28 UTC | |
by planetscape (Chancellor) on Sep 17, 2007 at 18:20 UTC | |
by subhankar (Novice) on Sep 17, 2007 at 08:29 UTC | |
by graff (Chancellor) on Sep 17, 2007 at 13:04 UTC | |
|
Re: Multidimensional Hash implementation/usage
by jwkrahn (Abbot) on Sep 17, 2007 at 08:39 UTC |