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
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.
|
|---|