Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Using the read function...

by basicdez (Pilgrim)
on Oct 08, 2001 at 20:14 UTC ( #117487=perlquestion: print w/replies, xml ) Need Help??

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

I want to read in a list of data, that looks similar to the following...
"DESTINATION" "localhost" "HDR" "R-ITEC" "" "ITDEC" "DEC" "PENDING" 08/29/01 18864 "EOS" "EOS"
To read this in with read and seperate out the following fields so that in perl code, I could assign the following values...
$Destination = "localhost" $Program_Type = "R-ITEC" $Program_Name = "" $Incoming_Foo = "ITDEC" $Decision = "PENDING" $Date_Applied = "08/29/01" $Appl_Number = "18864"
Please help me if you know what I should do. I am currently under an understanding that I can do something of the following...
while(<$cliendft>) { last if /^EOS$/; } close ($clientfd);
to read in the data stream, but I do not know how to seperate these fields into where I would want them to go to complete this process. Any help you could give me would be greatly appreciated. peace dez L

Replies are listed 'Best First'.
Re: Using the read function...
by thatguy (Parson) on Oct 08, 2001 at 21:04 UTC
    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
Re: Using the read function...
by John M. Dlugosz (Monsignor) on Oct 08, 2001 at 20:48 UTC
    If your quotes are necessary because your fields contain spaces or other special chars, don't use split. Rather, use a module that processes quotes and comma lists (you don't have commas, but you don't have to draw upon that feature. The quotes is the hard part). Search CPAN for "quote" and such. I know they exist, but am not familiar with specific modules.

    It would be simpler to assign to hash members, not variables, with the results. E.g.

    foreach (my $input) @data { my ($key, $value)= parseline ($input); $results{$key}=$value; }
    —John

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-08-19 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (141 votes). Check out past polls.

    Notices?