Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using the read function...

by thatguy (Parson)
on Oct 08, 2001 at 21:04 UTC ( #117509=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Using the read function...
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117509]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-08-01 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (27 votes), past polls