http://qs321.pair.com?node_id=117509


in reply to Using the read function...

here's my suggestion.. it uses the line number to determine what data it is looking at.

#!/usr/bin/perl -w use strict; use vars qw/ $Destination $Program_Type $Program_Name $Incomming_Foo $Decision $Date_Applied $Appl_Number $junk /; my $i; open(DATA," basicdez.dat") || die "Cannot open datafile!: $!\n"; while(<DATA>){ chomp; $i++; # if you do not need the quotes, uncomment the next line # $_=~ s/"//g; # use line number to decide what data goes where # if the current data is EOS then reset the linenumber to 0 if ($_ =~ /EOS/){ $i=0; } # line one is Destination if ($i eq 1){ ($junk,$Destination)=split(/ /, $_); print "Destination = $Destination\n"; # line two is unused # line three is Program_Type,Program_Name,Incomming_Foo } elsif ($i eq 3){ ($Program_Type,$Program_Name,$Incomming_Foo)=split(/ /, $_); print "Program_Type = $Program_Type\n"; print "Program_Name = $Program_Name\n"; print "Incomming_Foo = $Incomming_Foo\n"; # line four is unused # line 5 is Decision,Date_Applied,Appl_Number } elsif ($i eq 5){ ($Decision,$Date_Applied,$Appl_Number)=split(/ /, $_); print "Decision = $Decision\n"; print "Date_Applied = $Date_Applied\n"; print "Appl_Number = $Appl_Number\n"; } # lines six and seven should be EOS and caught by # earlier statement } close(DATA); exit;

This can be easily broken if the number of lines changes, but if your data never changes or keeps the same model then it should be fine.

Update: instead of line numbers, just parsing it all as one line:

#!/usr/bin/perl use strict; my $i; my $data; open(DATA," basicdez.dat") || die "Cannot open datfile: $!\n"; while(<DATA>){ chomp; unless ($_=~ /"EOS"/){ $data .= "$_"; } elsif ($i ne 1) { $data .="\n"; } if ($_=~ /"EOS"/){ $i=1; } } close(DATA); my @data=split(/\n/, $data); for(@data){ my($Destination,$Program_Type,$Program_Name,$Incomming_Foo, $Decision,$Date_Applied,$Appl_Number)=split; print "$Destination,$Program_Type,$Program_Name,$Incomming_Foo,"; print "$Decision,$Date_Applied,$Appl_Number\n"; } exit;

-p