Dear Monks,
I am a newbie to Perl and I have been trying to work with it since the last few days.

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 :

#!/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}; }
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 :
$CNT{substr($Fld67, 3, 5), $Fld1, $Fld15}++; $COST{substr($Fld67, 3, 5), $Fld1, $Fld15} += $Fld14;
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.

Although I am able to get the values, any help in understanding the hash mechanism implemented here, would be very helpful.


Thanks & regards,
Subhankar


In reply to Multidimensional Hash implementation/usage by subhankar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.