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

I have a flat file db and I would like to extract a column (via CGI).
I am trying to use the following code:
#!/usr/bin/perl -w use strict; use CGI ':standard'; my ($oneaye,$twobee,$threesee,$fourdee,$fiveee); my line; print "Content-type: text/html\n\n"; open (PAGE, "$data") or die "Can't open $data: $!"; while (<PAGE>) { ($oneaye,$twobee,$threesee,$fourdee,$fiveee) = split "\t",$line; unless ($threesee eq "") { print "three = $threesee<br>"; print "four = $fourdee<br>"; } }
I was hoping this would produce a nice list, however this doesn't seem to pick up the variables.

Can someone explain my mistake(s)?

Many thanks

Replies are listed 'Best First'.
Re: extracting a list from a file
by jasonk (Parson) on Mar 05, 2003 at 16:22 UTC

    The mistake is that you are splitting on $line, which you never assigned a value to, so your variables are always empty. Perhaps you meant to say while($line = <PAGE>) {.

Re: extracting a list from a file
by perlguy (Deacon) on Mar 05, 2003 at 16:40 UTC
    Try this:
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); print header(); my $data = 'sample.db'; open (PAGE, $data) or die "Can't open $data: $!"; while (my $line = <PAGE>) { my @columns = split "\t", $line; if ($columns[2]) { print "three = $columns[2]<br>"; print "four = $columns[3]<br>"; } }
Re: extracting a list from a file
by Anonymous Monk on Mar 05, 2003 at 17:13 UTC
    Thanks for your replies - I'm back on track.
    v. much appreciated.
Re: extracting a list from a file
by hgolan30 (Initiate) on Mar 05, 2003 at 17:26 UTC
    #!/usr/bin/perl -w use Carp; use CGI; my $query = new CGI; print $query->header(-expires => '-1d'); print <<"EOHTML"; <HTML> <HEAD> </head> <BODY > EOHTML ; my ($oneaye,$twobee,$threesee,$fourdee,$fiveee); my line; open (PAGE, "$data") or die "Can't open $data: $!"; while (<PAGE>) { ($oneaye,$twobee,$threesee,$fourdee,$fiveee) = split "\t",$line; unless ($threesee eq "") { print "three = $threesee<br>"; print "four = $fourdee<br>"; } } print $query->end_html;