Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Confused Beginner: searching and displaying information from a dat file

by Anonymous Monk
on Nov 05, 2001 at 20:20 UTC ( [id://123341]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I was wondering if you could help me with a problem my boss has given me to solve and as I am a beginner in perl I am having a few problems. I have to create a perl script that searches through a single dat with one field on each line and what I need it to do is pick out the first 11 lines (as these form one record) of the dat file, then I have to create a search engine where he can type in a keyword, it will read in the dat file and then take all the records with the associated keyword and display them in HTML. I tried downloading and following a search.pl module but the results were not what I needed. The problem seems to be that when I do the search it just picks out and displays the keyword and not the set of records, I can?t seem to get the perl script to take the first 11 lines and put them into a record and then display them in a format that is clear.

Any help would be much appreciated.

Thanks

Jason

Edit kudra, 2001-11-05 Changed title; added markup

  • Comment on Confused Beginner: searching and displaying information from a dat file

Replies are listed 'Best First'.
Re: Confused Beginner: searching and displaying information from a dat file
by blackmateria (Chaplain) on Nov 05, 2001 at 20:29 UTC
    You need to post some code and sample data; otherwise it's going to be very hard for anyone to help you.

      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.

Re: Confused Beginner: searching and displaying information from a dat file
by dirthurts (Hermit) on Nov 05, 2001 at 22:45 UTC
    can I have your job?

    But seriously, have you checked into the substr function?

Re: Confused Beginner: searching and displaying information from a dat file
by msemich (Novice) on Nov 05, 2001 at 23:34 UTC
    Errr, I agree with the other in that you need some sample code; but I feel you are trying too hard in the first place. Do you have access to a database or will you have to write these to disk? You can use the integral search functions on a record, then :).
Re: Confused Beginner: searching and displaying information from a dat file
by Anonymous Monk on Nov 07, 2001 at 20:51 UTC
    Wow BlackMateria, you are a star, it works a treat and I have read up on all the bits and understand now how it works!! Thankyou so much. Now I have another query...if you can help me out on this one I would be forever in your debt! I now need to run a HTML search which asks for a keyword which will then open up my new perl script and display the results in HTML. At the moment I put a peice of data in $keyword and it finds all the records when I run the script, but how do I get the HTML to overwrite that keyword so it can change everytime the need a different search? Hope that makes sense!! So in essense with everything put together, a person gets a HTML page which asks what the would like to search for, they put in a keyword, the result is then shown on a html for that particular file. Cheers Jason
      Check out CGI.pm and friends on CPAN. Documentation for CGI.pm can be found here, among other places. Hope this helps!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-24 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found