Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

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


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

You need to post some code and sample data; otherwise it's going to be very hard for anyone to help you.
  • Comment on Re: Confused Beginner: searching and displaying information from a dat file

Replies are listed 'Best First'.
Re: Re: Confused Beginner: searching and displaying information from a dat file
by Anonymous Monk on Nov 06, 2001 at 14:12 UTC

    OK here is the sample of data as it looks in the DAT file:

    PC025 A A TELCO LEFT HAND SIDE J0007 RJ45 EARLESS N/A N/A N/A N/A 50 WAY MOUNT CONNECTOR FEMALE BT009 PC026 A A TELCO RIGHT HAND SIDE J0007 RJ45 EARLESS N/A N/A N/A N/A 50 WAY MOUNT CONNECTOR FEMALE BT009 PC029 A A TELCO LEFT HAND SIDE J0007 RJ45 EARLESS N/A N/A N/A N/A 50 WAY MOUNT CONNECTOR MALE BT010

    And this is what I have come up with in the form of code...please don't laugh...hehe!!!:

    open (DATFILE, "prod.dat") or die "I couldn't get at log.txt"; for $line (<DATFILE>) { print "$line <br>"; } close DATFILE;
    I know it don't look much but the books I have referenced don't seem to help, and there is noone else I know with any perl knowledge.
    Hope this makes things a bit clearer
    Thanks

    2001-11-06 Edit by Corion : Changed formatting to use opening and closing CODE tags

      So you are already halfway. You just need to plough in a few online docs. Try (at the commandline) 'perldoc perlop', and perlre for searching/replacing, and 'perldoc perlrun' for nifty commandline switches -- be sure to look up -i and -p. Oh yeah, don't forget to read all about ARGV in perlvar.
      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://123343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-24 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found