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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117509]
help
Chatterbox?
[Eily]: Maybe the puns in his dialogues were somehow kept through translation, but I can tell he lost his accent
[LanX]: qui?
[Eily]: Astérix
1nickt gives up on finding a Perl YAML parser that supports hash merging. In 2017.
[LanX]: Oh yes true, he lost his accent! xD
[Eily]: and since his name is a modified version of astérisque (asterisk), which is a typographical symbol, I think it's even funnier that he would lose a diacritic
[Eily]: the pun doesn't work so well with "he lost a diacritic" though :P
[LanX]: Obelix might offer to find it again ...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2017-05-24 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?