Hi, I posted a couple of days ago asking how to make array sout of a text file, I tried it but found that it didn't exactly fit my needs. What I need to be able to do is input a text file and process the contents of it - being able to grab any piece of data and output it in any order. here's an example of a text file that i'll be using...
HDR,831 Reconciliation,200008081441EST BGN,11,000000158,20000808,10520000 OTI,GH,BT,WEDI620000802,159 AMT,2,231644 AMT,BT,231644 AMT,NP,0 QTY,46,10 QTY,53,0 QTY,54,10 QTY,55,0 TED,ZZZ,BATCH AWAITING 831 OTI,TR,CK,0021000095,159,000101416 TED,ZZZ,MOD CHECK ON RDFI-ID FAILED
Note: there could be multiple lines that begin w/QTY, TED and OTI. What I did was write a PERL script that inputs the file, creates an array of six items (for those that don't have six items it fills the empty ones in as blank), it then goes and counts how many of the first item appears. Here is the code I have written thus far (i know it's not the most efficient but it works!)
#!/usr/local/bin/perl5 #PERL Script #1. Input the text file containing the formatted info open (FILE,"<824.txt") || die "Can't open file $!"; @lines = <FILE>; #initialize the counters $HDRcount = 0; $BGNcount = 0; $OTIcount = 0; $AMTcount = 0; $QTYcount = 0; $TEDcount = 0; #Create the main array foreach (@lines) { chop; ($key, $first, $second, $third, $fourth, $fifth) = (split(/,/)); $key = "" if !defined($key); #I know this part isn't needed bu +t i'd rather not get errors $first = "" if !defined($first); $second = "" if !defined($second); $third = "" if !defined($third); $fourth = "" if !defined($fourth); $fifth = "" if !defined($fifth); $sixth = "" if !defined($sixth); print("Key=$key 1=$first 2=$second 3=$third 4=$fourth 5= +$fifth\n"); #Search through firstElementArray counting up each instance of # HDR, BGN, OTI, AMT, QTY, TED if ($key eq "HDR") { $HDRcount++; #create an array here, similiar to @HDR($first, $second, $thir +d) } $BGNcount++ if ($key eq "BGN"); $OTIcount++ if ($key eq "OTI"); $AMTcount++ if ($key eq "AMT"); $QTYcount++ if ($key eq "QTY"); $TEDcount++ if ($key eq "TED"); } #test to make sure it works print("HRD=$HDRcount BGN=$BGNcount OTI=$OTIcount AMT=$AMTcount QTY +=$QTYcount TED=$TEDcount\n"); close(FILE);
What I would like to be able to do is go in and output the data in any format. For example, to print out the second and third items of all the QTY's and print the third item in the HDR and the fourth and fifth of all of the OTIs. I've been looking at associative array's/hashes and everything else and I really don't know what to use. Any suggestions?

In reply to Creating the right type of array... by Anonymous Monk

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.