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

Hi
Using a URL, how can I parse one line of variables from a multi-line tab-delimited text file?

For instance:

http://www.domain.com/cgi-bin/read.pl?order=12345

should pull

12345 date name email

from this text file

12345 date name email
12346 date name email
12347 date name email
12348 date name email
12349 date name email
12350 date name email

and print on a new page.

Thanks in advance!
Jerry
  • Comment on Parse one line of variables in multi-line tab delimited text file

Replies are listed 'Best First'.
Re: Parse one line of variables in multi-line tab delimited text file
by wind (Priest) on Mar 17, 2011 at 18:40 UTC

    Have you tried anything thus far mate?

    Just read up on CGI for the web aspect to the problem, and do basic file processing like below for searching for the record.

    use strict; use warnings; while (<DATA>) { chomp; my @vals = split "\t"; if ($vals[0] == 12345) { print "Bingo"; last; } } __DATA__ 12345 date name email 12346 date name email 12347 date name email 12348 date name email 12349 date name email 12350 date name email
      This is REAL close, thank you.

      Instead of "Bingo," though, I would like it to print out all of the variables on that line:

      12345 date name email
        EUREKA!

        open (FILE, "<$ORDERS");
        while (<FILE>) {
        chomp;
        my @vals = split "\t";
        $order = $vals[0];
        $Date= $vals1;
        $name= $vals2;
        $email= $vals3;

        if ($vals[0] == $order) {
        print "$order";
        print "$Date";
        print "$name";
        print "$email";
        last;
        }

        }
        close (FILE);
        exit;

        THANK YOU!
      Thanks, I have the web part figured out. I just would like help with the code that actually stores these values in a string.

      Here's what I have so far, but it only prints out a dump of the entire text file:

      print "Content-type: text/plain\n\n";

      $ORDERS="../../orders.txt";

      open (FILE, "<$ORDERS");
      while (<FILE>) {
      chomp;
      ($number, $Date, $name, $email) = split("\t");
      print "Order: $number\n";
      print "Date of Order: $Date\n";
      print "Name: $name\n";
      print "Email: $email\n";
      print "---------\n";
      }
      close (FILE);
      exit;
Re: Parse one line of variables in multi-line tab delimited text file
by locked_user sundialsvc4 (Abbot) on Mar 17, 2011 at 23:05 UTC

    As you do more with Perl, also keep in mind CPAN ... the vast contributed-code library at http://search.cpan.org.   No matter what you are doing, it has probably been done before, and there is probably “an embarrassment of choices” in CPAN for doing even very complicated things.   In other words, as a general notion, “search before you write, because you might not have to write nearly as much original material as you first thought.”

    “There’s More Than One Way To Do It™” ... including ways that you might never have thought of until you searched.