Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Re: Confused Beginner: searching and displaying information from a dat file

by blackmateria (Chaplain)
on Nov 06, 2001 at 20:49 UTC ( [id://123616]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Confused Beginner: searching and displaying information from a dat file
in thread Confused Beginner: searching and displaying information from a dat file

Well, you're reading in the data file, and spitting out some HTML; that's a good start. I agree with jeroenes that you need to read some documentation. You definitely need to look at the basics though: perldata, perlsyn, perlfunc, and perlop. You might also try the books, Learning Perl (beginner-oriented) and Programming Perl (much steeper learning curve, but good if you've programmed before). This will all seem overwhelming and give you a headache at first, but once you've been over it a few times, you'll be much better equipped to solve problems in perl.

Now then, your .DAT file's format, with no distinction between record separator and field separator (i.e. each field is on a separate line) doesn't mix very well with line-oriented data processing, which is what most of those perl tutorials are going to talk about. If I were you, I would try to convert the .DAT file into a tab-delimited fields format (it doesn't look like your data will have any tabs in it, so this should be safe). If you can't do that to the external file, you can still convert it for use internally by your perl program:

#!/usr/bin/perl -w use strict ; my $filename = 'sample.dat' ; open (DATAFILE, "<$filename") or die "$0: cannot open file \"$filename\" for reading: $!\n" ; my (@record, @records) ; while (<DATAFILE>) { tr/\r\n//d ; push @record, $_ ; if (@record == 11) { push @records, join ("\t", @record) ; @record = () ; } } close (DATAFILE) ; die "$0: incomplete record (line count = ", scalar @record, ") at end +of file \"$filename.\"\n" unless @record == 0 ;

This code snippet will read each group of 11 lines into an array (@record), then convert it to a single tab-delimited line and add it to the array @records. At the end, @records will contain your entire file in tab-delimited format. Now that the program has an internal copy of your file in a sensible format, it's easy to do fun things with it, like print it all out (print map {"$_\n"} @records) or search through it looking for keywords (print map {"$_\n"} grep /\Q$keyword\E/, @records). To understand all of this, look up grep and map in perlfunc, and read some of that other material people have posted for you.

Anyway, I hope this is enough to get you started. If you have any more specific questions, be sure to post them, or create an account and ask in the Chatterbox.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found