Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Reading in data that is by jryan
in thread Reading in data that is by basicdez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-19 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found