Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Reading in data that is

by jryan (Vicar)
on Oct 10, 2001 at 19:49 UTC ( #118024=note: print w/replies, xml ) Need Help??

in reply to Reading in data that is

Your code has a number of errors in it. Consider the following:

#!/usr/bin/perl use strict; my $i; my $data; open(DATA," basicdez.dat") || die "Cannot open datfile: $!\n"; # this entire loop is pointless. its almost never a good # idea to read an entire file into a single string, unless # you are going to be spitting it right back out. while(<DATA>){ chomp; unless ($_=~ /"EOS"/){ $data .= "$_"; } elsif ($i ne 1) { $data .="\n"; } if ($_=~ /"EOS"/){ $i=1; } } close(DATA); my @data=split(/\n/, $data); # i find it odd that although you name every element of your # list, you don't name the loop variable. Either do one, or # both. Be consistant! for(@data){ my($first_name,$last_name,$address,$city,$state,$phone)=split; print "$first_name,$last_name\n"; print "$address\n"; print "$city, $state\n"; print "$phone \n"; } # not needed exit;

Heres how I would have done what you wanted (with explanations)

#!/usr/bin/perl -w # use warnings! use strict; # suck the entire file into an array, rather than just # putting it into a file. open(DATA," basicdez.dat") || die "Cannot open datfile: $!\n"; my @data = <DATA>; close(DATA); # remove your eof element. If you can help it, take it out of the # dat file completely, so you can remove this line of code. @data = @data[0..@data-1]; # name and scope your loop variable for my $data (@data) { # remove those pesky quotes. If you can help it, take those out # of the dat file too! If i remember correctly, they are # needed in vb; however, Perl isn't a weak language like # vb is. It can handle raw text ;) You should delimit # your text with a single obscure character (the standard # obscure character (the standard for flat dbs is the # pipe | symbol.) # However, given your data as it is now, this will work: $data =~ s/" "/""/g; $data =~ s/""/|/g; $data =~ tr/"//d; # split on the newly created pipe delimited entry. my($first_name,$last_name,$address,$city,$state,$phone) = split /\|/, $data; print "$first_name,$last_name\n"; print "$address\n"; print "$city, $state\n"; print "$phone \n"; }

There you have it. I hope I was clear enough; if I wasn't, please reply. Good luck!

Update: fixed tr and split, thanks to projekt21

Replies are listed 'Best First'.
Re: Re: Reading in data that is
by projekt21 (Friar) on Oct 10, 2001 at 20:56 UTC

    Your split won't work correctly: some fields contain whitespace (e.g. address).

    alex pleiner <>
    zeitform Internet Dienste

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2023-12-09 23:59 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.