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

Help with Reading in of a flat file...

by basicdez (Pilgrim)
on Jan 18, 2002 at 04:15 UTC ( [id://139680]=perlquestion: print w/replies, xml ) Need Help??

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

Okay, here is what I am trying to do. I need to read in the data beginning after "TTY" and read this in until the next "TTY" or "EOS", my dilemma is how do I do that?

Please oh please wise monks brothers and sisters, help me if you can...

Here is the file that I am trying to read in...

"TTY" "A1" "" "CBI" "*********************************** APPLICANT-C1 ****** +*********************************** EQUIFAX 5.0 for application 4309 *526 EQUIFAX CREDIT INFORMATION SERVICES P O BOX 740241 1150 LAKE HEARN DRIVE STE 460 ATLANTA GA 303740241 + (800) 685-1111 BEACON: 623 00022-ACCOUNT NOT PAID AS AGREED, PUBLIC RECORD, OR COLLECTION AGENC +Y FILLING 00018-NUMBER OF ACCOUNTS CURRENTLY OR IN THE PAST NOT PAID AS AGREED 00010-RLTNSHP OF BALANCE TO HIGH CREDIT ON BANK/NAT OR OTHER REVOLVI +NG/OPEN ACCTS 00013-LENGTH OF TIME (OR UNKNOWN TIME) SINCE ACCT PD AS AGREED OR TR +ADE NARR RPTD D.A.S. SCORE: (CENTRAL MODEL): 704 08023-NUMBER OF ACCOUNTS SHOWING PAYMENT LATE BY 90 DAYS OR MORE / P +UBLIC RECORD ITEMS END OF REPORT EQUIFAX AND AFFILIATES 08/29/01 SAFESCANNED. *********************************** APPLICANT-, FILE-0 *************** +******************** END OF REPORT EQUIFAX AND AFFILIATES 08/29/01 SAFESCANNED. " "TTY" etc....
I am reading in this file as though it were all on one line, but it is not coming from my project that way. Here is the code that I am using...
elsif (/"TTY"/) { $TTY_Ind = 1; $toggle = 34, next if /^"TTY"\s*$/; } last if /"EOS"/;
How can I get this to read the data in and not worry about the data running onto the next line. I want you to know that if I go in the vi editor and delete all the end of line characters (of which I cannot see - they are mysterious), I can get this file read in perfectly. Please help me with this if you have knowledge. I am racking and racking and racking my brain on this one. PS Don't panic on the information in here it is all false...

peace, LOVE and ((code))

basicdez

Replies are listed 'Best First'.
Re: Help with Reading in of a flat file...
by trs80 (Priest) on Jan 18, 2002 at 04:47 UTC
    I have to admit I am confused by your post. Your code
    snippet is very vague and how/where some of the variables
    are used is a mystery to me, so keep this in mind as you
    read my reply.
    If the file you are reading contains records between "TTY"
    you would be better off reading the entire file in (if it is
    small enough) and then doing a split on it, like so:
    @sections = split(/\"TTY\"/,$file_contents); Now @sections will contain each chunk of data and you can
    process each of those as needed. If your concern is reading
    in the file and breaking on the end of the line, I would
    recommend using a hex editor to view the original file and
    determine exactly what the end of line character is, then you
    use that to split again inside of your sections loop
    You can set the input record seperator if you are determined
    to read in each line via a while, using the $/ global special
    variable.

    Again this is based on my vague understanding what you are
    attempting to do.
Re: Help with Reading in of a flat file...
by Anonymous Monk on Jan 18, 2002 at 04:59 UTC
    <code>while( <> ){ print if /"TTY"/...(/"TTY"/||/"EOS"/); }<code>
Re: Help with Reading in of a flat file...
by Ineffectual (Scribe) on Jan 18, 2002 at 04:49 UTC
    Well, I'm not quite sure what you're doing here (when I usually read in text files I tend to use a on/off switch), but if you're having problems with characters denoting linebreaks, you can either do chomp(); to remove \n or if it's a MS Word document, it probably has a control-M in it, which can be parsed out using /r. Hope this helps.

    'Fect
Re: Help with Reading in of a flat file...
by FoxtrotUniform (Prior) on Jan 19, 2002 at 01:24 UTC

    Note: untested

    { local $/ = "TTY"; open FILE, "<", $filename or die "open $filename: $!\n"; my @ttys = <FILE>; my @data = (); foreach (@ttys) { push @data, $_; last if /EOS/; } close FILE or die "close $filename: $!\n"; }
    --
    :wq

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-28 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found