basicdez has asked for the wisdom of the Perl Monks concerning the following question:

Please help me out if you can... I currently want to read in the following data file.
"PER" "A1" "Denise Johnson" "red,orange,yellow" 09/25/2001 "PER" "A2" "Desiree Smith" "yellow,blue-green" 03/06/1970 "EMP" "A1" "X & Y Bank" "Teller" 05/17/1994 "Brian's Point" "WI" 54997 24000 +.00 "EMP" "A2" "Acme Corp" "Computer Programmer" 07/24/1997 "Maxwell" "WI" 53224 + 45000.00 "EOS"
What I would like to be able to do is read in all of the PER info and EMP info where the first field (A1 or A2) are matching and then put them into a similar hash and thus come through with the following output.
--------------------- Entity = A1 Name = Denise Johnson Color = red Color = orange Color = yellow Date = 09/25/2001 ---------------------- Entity = A2 Name = Desiree Smith Color = yellow Color = blue-green Date = 03/06/1970 ------------------------ Entity = A1 Employment = X & Y Bank Job Title = Teller Date of Hire = 05/17/1994 Location = Brian's Point, WI 54997 Salary = 24000.00 ------------------------ Entity = A2 Employment = Acme Corp Job Title = Computer Programmer Date of Hire = 07/24/1997 Location = Maxwell, WI 53224 Salary = 45000.00
Here is my code so far, but it is not giving me the correct result (actually it is giving me NO RESULT). Please help me if you can, but do not laugh to hard at me, please....
#!/usr/bin/perl-Tw use strict; use Text::ParseWords; open(DATA,"basicdez.dat") || die "Cannot open datfile: $!\n"; my @data; my $i = 0; my $new_record = 0; while (<DATA>) { chomp; last if /^"EOS"$/; { @data = &quotewords('\s+', 0, $_); } if ((/^PER\s*$/) && ($data[0]) && ($data[1]) && ($data[2]) && ($data +[3])) { my @colors=split(/,/, $data[2]); print "---------------------\n"; print "Entity = $data[0]\n"; print "Name = $data[1]\n"; for (@colors){ # print each color print "Color = $_\n"; print "Date = $data[3]\n"; } if ((/^EMP\s*$/) && ($data[0]) && ($data[1]) && ($data[2]) && ($data +[3]) && ($data[4]) && ($data[5]) && ($data[6]) && ($data[7])) { print "------------------------\n"; print "Entity = $data[0]\n"; print "Employment = $data[1]\n"; print "Job Title = $data[2]\n"; print "Hire Date = $data[3]\n"; print "Location = $data[4], $data[5] $data[6]\n"; print "Salary = $data[7]\n"; } } }
I do need to use the PER and EMP as seperators to sort out the data, so if you could tell me a way to put this information together without to much stress that would be great. Anything you could do to help out would be awesome at this point. peace, LOVE and ((hugs)) as always dez L

Replies are listed 'Best First'.
Re: Help with creating output to screen...
by traveler (Parson) on Nov 07, 2001 at 04:47 UTC
    Your datafile makes it look as though "PER" and "EMP" are on lines separate from the data, but
    { @data = &quotewords('\s+', 0, $_); } if ((/^PER\s*$/) && ($data[0]) && ($data[1]) && ($data[2]) && ($data +[3]))
    makes it look as though you might sort of think they are on the same line. I think you need to do the following (expressed as pseudocode; you still need quotewords, etc.):
    while (<>){ last if EOS if (PER){ read a PER line and process it } elsif (EMP){ read an EMP line and process it } }
    HTH, --traveler
Re: Help with creating output to screen...
by runrig (Abbot) on Nov 07, 2001 at 04:45 UTC
    Your 'PER' and 'EMP' separators are on separate lines from the rest of the data, so when you match, e.g., /^PER/, there is nothing ELSE in @data (except $data[0] eq 'PER').