Searching data from file 
Author Message
 Searching data from file

The following is a script that I would like to read a file and search for a
state based on the link that someone clicks on a map of the US. Each state
will have its own link. (e.g.
http://www.*-*-*.com/ ) The file is a
flat ASCII file with a "::" delimiter. I have written this code and cannot
seem to get it to work. The $cstate variable will pass through, but the
variables from the file will not. Any help is appreciated.

Gregory Fox

----------- BEGIN CODE -----------------

#!/bin/perl
print "Content-type: text/html\n\n";
$datafile = "/www/htdocs/lts/files/consultants.dat";

($field_name, $cstate) = split (/=/, $ENV {'QUERY_STRING'});

&lookup_consultant;

sub open_consultantfile{
          open(FILE,"<$datafile");

           close FILE;

           $x=0;

            s/^ //;
                ($fname[$x], $lname[$x], $address1[$x], $address2[$x],
$city[$x], $state[$x], $zip[$x], $phone[$x], $fax[$x], $email[$x]) =
split(/::/, $item);
            $x++;
             }

Quote:
}

sub lookup_consultant{
&open_consultantfile;
 for ($x=0; $x<=$numlines; $x++){

  if ($cstate eq $state[$x]){

  # This is where the HTML will go

                  print "$cstate, $fname, $lname, $address1, $address2,
$city, $state, $zip, $phone, $fax, $email\n";

          $x = $numlines;
          }
      }

Quote:
}



Tue, 13 Aug 2002 03:00:00 GMT  
 Searching data from file
Change your first line into

#!/bin/perl -w  # issues warnings

Use the de{*filter*} by calling

perl -d your_program.

Type l for list, n to execute next statement, s to step into sub, x $scalar
to print $scalar etc.

Michael



Tue, 13 Aug 2002 03:00:00 GMT  
 Searching data from file

        This should be $numlines=$#list;, shouldn't it ?

If this solves your problem I can't tell but maybe it helps.

Markus

--
Markus Fischer,  http://josefine.ben.tuwien.ac.at/~mfischer/

PGP Public  Key: http://josefine.ben.tuwien.ac.at/~mfischer/C2272BD0.asc
PGP Fingerprint: D3B0 DD4F E12B F911 3CE1  C2B5 D674 B445 C227 2BD0



Tue, 13 Aug 2002 03:00:00 GMT  
 Searching data from file

Quote:

> The following is a script that I would like to read a file and search for a
> state based on the link that someone clicks on a map of the US. Each state
> will have its own link. (e.g.
> http://www.*-*-*.com/ ) The file is a
> flat ASCII file with a "::" delimiter. I have written this code and cannot
> seem to get it to work. The $cstate variable will pass through, but the
> variables from the file will not. Any help is appreciated.

A general observation: the program below doesn't appear to match either
your functional description or its internal documentation.  Based on
your problem description I would have thought the challenge was to
identify lines in consultants.dat that pertain to TX.  If you're really
doing that it's not very apparent and whatever approach you are using
(not mentioned in the documentation to your program!) doesn't seem very
direct and to the point.

At first blush, this looks to me like a case for running grep in
backticks against consultants.dat to capture the lines in that file to
an array, doing some HTML setup to create the page, and then iterating
through the results array, parsing on the ::s into fields, printing the
results out in a table.  (One would, of course, put in the proper
safeguards to avoid passing {*filter*} badness to grep, etc.)  I'd love to
see a similar description of what you believe your program is going to
achieve.

Quote:

> Gregory Fox

> ----------- BEGIN CODE -----------------

> #!/bin/perl
> print "Content-type: text/html\n\n";
> $datafile = "/www/htdocs/lts/files/consultants.dat";

> ($field_name, $cstate) = split (/=/, $ENV {'QUERY_STRING'});

> &lookup_consultant;

> sub open_consultantfile{

Looks to me as though this sub is doing a whole hell of a lot more than
simply opening a file!  Calling it "open_consultantfile" is at best
seriously misleading, don't you think?
Quote:
>           open(FILE,"<$datafile");

>            close FILE;

>            $x=0;

>             s/^ //;
>                 ($fname[$x], $lname[$x], $address1[$x], $address2[$x],
> $city[$x], $state[$x], $zip[$x], $phone[$x], $fax[$x], $email[$x]) =
> split(/::/, $item);
>             $x++;
>              }
> }

> sub lookup_consultant{
> &open_consultantfile;
>  for ($x=0; $x<=$numlines; $x++){

>   if ($cstate eq $state[$x]){

>   # This is where the HTML will go

>                   print "$cstate, $fname, $lname, $address1, $address2,
> $city, $state, $zip, $phone, $fax, $email\n";

>           $x = $numlines;
>           }
>       }

> }



Tue, 13 Aug 2002 03:00:00 GMT  
 Searching data from file

Quote:
> seem to get it to work. The $cstate variable will pass through, but the
> variables from the file will not. Any help is appreciated.

I would guess that is because you first declare it in a subroutine and that
it loses scope at the end of the subroutine.  Here is the same prg.

#!/bin/perl

use strict;

print("Content-type: text/html\n\n");
$datafile = '/www/htdocs/lts/files/consultants.dat';


($qfieldname, $qstate) = split(/=/, $ENV{'QUERY_STRING'});


close(SRC);

my ($fname, $lname, $address, $address2, $city, $state, $zip, $phone, $fax,
$email);

  ($fname, $lname, $address, $address2, $city, $state, $zip, $phone, $fax,
$email) = split(/::/, $_);
  if ($qstate eq $state) {
     #HTML stuff
     print("$state, $fname, $lname, $address, $address2, $city, $state,
$phone, $fax, $email\n");
  };

Quote:
};



Fri, 16 Aug 2002 03:00:00 GMT  
 Searching data from file
Greg,

I have use the following code to search for information in a flat file
and it works for me. Allot less code than you have here!
Let me know if this helps you out.


Bell Canada

#!/bin/perl

print "Content-type: text/html\n\n";
$datafile = "/www/htdocs/lts/files/consultants.dat";

($field_name, $cstate) = split (/=/, $ENV {'QUERY_STRING'});

open(FILE,"<$datafile");

foreach $item (<FILE>) {
  $item =~ s/^ //g;
  ($fname, $lname, $address1, $address2, $city, $state, $zip, $phone,
$fax, $email) = split(/::/, $item);
  if ($item =~ /$cstate/) {
    #do your HTML stuff
    print $fname $lname ..... (and so on...)
  }

Quote:
}

close(FILE);
Quote:

> The following is a script that I would like to read a file and search for a
> state based on the link that someone clicks on a map of the US. Each state
> will have its own link. (e.g.
> http://libertymarketing.com/cgi-bin/clickmap.cgi?state=TX) The file is a
> flat ASCII file with a "::" delimiter. I have written this code and cannot
> seem to get it to work. The $cstate variable will pass through, but the
> variables from the file will not. Any help is appreciated.

> Gregory Fox

> ----------- BEGIN CODE -----------------

> #!/bin/perl
> print "Content-type: text/html\n\n";
> $datafile = "/www/htdocs/lts/files/consultants.dat";

> ($field_name, $cstate) = split (/=/, $ENV {'QUERY_STRING'});

> &lookup_consultant;

> sub open_consultantfile{
>           open(FILE,"<$datafile");

>            close FILE;

>            $x=0;

>             s/^ //;
>                 ($fname[$x], $lname[$x], $address1[$x], $address2[$x],
> $city[$x], $state[$x], $zip[$x], $phone[$x], $fax[$x], $email[$x]) =
> split(/::/, $item);
>             $x++;
>              }
> }

> sub lookup_consultant{
> &open_consultantfile;
>  for ($x=0; $x<=$numlines; $x++){

>   if ($cstate eq $state[$x]){

>   # This is where the HTML will go

>                   print "$cstate, $fname, $lname, $address1, $address2,
> $city, $state, $zip, $phone, $fax, $email\n";

>           $x = $numlines;
>           }
>       }

> }



Mon, 19 Aug 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Searching data from file

2. search a data base file

3. In search of file searching script,output,every line occurance occurs

4. searching in complex data structures

5. Perlmenu/DBI-Mysql Data-Entry/Edit sample searched

6. Quick way to search data of Asoc. Array

7. PERLFUNC: study - optimize input data for repeated searches

8. searching complex data structure

9. perlfunc study - optimize input data for repeated searches

10. PERLFUNC: study - optimize input data for repeated searches

11. Data Search Question

12. problem searching fielded data

 

 
Powered by phpBB® Forum Software