Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Parse Pipe Delimited Text

by tinman (Curate)
on Apr 03, 2001 at 03:27 UTC ( [id://69145]=note: print w/replies, xml ) Need Help??


in reply to Parse Pipe Delimited Text

I see three main problems that you need to solve here

firstly, you need to parse a pipe delimited file.

Ok, for this, you can use the CPAN module Text::CSV_XS
Why this, you ask ? isn't CSV for "comma separated variables?".. true enough, but inside this wonderful module, you can also specify the separator character, which in your case would be a pipe.

You could of course, also roll your own, with a simple call to the split function such as (this is very simplistic, probably too much so)

open(DATA,"myfile") or die "No, can't open the file"; while(<DATA>) { my @elements = split('\|',$_); # do something with the elements here }

Your second problem is to find out when a new file has arrived..

First, store the modified date of the file with a call to the Perl inbuilt function stat, or by using the File::Stat module.

Whenever your Perl script wakes up (cron job? or NT 'at' scheduler call)and decides to process the file, check the last modified date that you stored, with the one of the file. If they differ, ie: if the FTP folder file has a more recent date, that means you should parse the file again...

Well, the third problem, outputting to an HTML table, is sort of simple, I think.. for completeness sake, I'll put a small pseudo-code like segment here that describes how it would be done...please note: this is untested

open(INPUT,"myfile") or die "Can't open file"; print "<html><head><title>My page</title></head><body>"; print "<table>"; while(<INPUT>) { my @elements = split('\|',$_); print "<tr>"; foreach my $el(@elements) { print "<td>$el</td>"; } print "</tr>"; } print "</table></body></html>";

HTH
Update: Fixed the code segments.. ack, can't believe I didn't put that in :(.. much respect to MrNobo1024
Update 2: Changed the name of the file handle from the reserved DATA to INPUT. DATA still works, but its not very good practice, as pointed out by others.

Replies are listed 'Best First'.
Re: Re: Parse Pipe Delimited Text
by MrNobo1024 (Hermit) on Apr 03, 2001 at 06:09 UTC
    Don't forget that the first argument to split is a regex, even if it looks like a string. split '|' dosen't work right, so try split '\|'.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 03:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found