Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Using elements to search a file

by GC (Initiate)
on May 06, 2009 at 15:58 UTC ( #762312=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to take input from a form ($string), look up that in a file ($inputs), get every space-separated entry on the line behind the $string, and put them in an array (@contractors).
All of that works fine.
The next step is to take the elements of that array, and look each one up in a second file ($continputs), and print out the results.
This is where I'm having the problem.
#!perl -w use strict; use CGI qw(:standard escapeHTML); ############# print header(), start_html("Search Results"); ############ my $string = param("submit_zip"); my $inputs = 'zip-acc.txt'; open( my $in, '<', $inputs) or die "can't open $inputs.\n"; while ( <$in> ) { if ( $_ =~ m/^$string(.*)/ ) { print "$1\n"; print br; print br; my @contractors = split(m#\s+#, $1); my $continputs = 'acc-addy.txt'; my $contractors; foreach $contractors(@contractors) { open( my $contin, '<', $continputs) or die "Cannot open $i +nputs.\n"; while ( <$contin> ) { if ( $contractors =~ m/^$string(.*)/ ) { my @results = split(m#\s+#, $1); print br; print @results; } } close( $contin ); } } } close( $in ); ############ print end_html(); ############

Replies are listed 'Best First'.
Re: Using elements to search a file
by jpearl (Scribe) on May 06, 2009 at 17:29 UTC
    Well, you separate out:
    my @contractors = split(m#\s+#, $1);
    where $1 is actually everything after $string. But then you try in your while( <$contin> ) { loop  if ( $contractors =~ m/^$string(.*)/ ) Depsite the fact that @contractors has already been regexed away from $string. Its hard to visualize without seeing some example input and output you are after, but you might not want to be matching $string multiple times like that, especially if $contractors is just an element of the array of stuff *after* $string.
      Example:

      User input: 11111

      Will find this line in the first file: 11111 1010101 1010102 1010103 1010104 1010105 1010106

      Which puts: 1010101 1010102 1010103 1010104 1010105 1010106 into an array.

      Each of those will be used as search criteria in the second file, the irst should read this line: 1010101 Address Line 1 ,Address Line 2 ,Phone Number ,Fax Number,

      So I can print the information.

      If that makes sense.

        That does make sense. However, I think your problem is in this loop:
        while ( <$contin> ) { if ( $contractors =~ m/^$string(.*)/ ) { my @results = split(m#\s+#, $1); print br; print @results; } } close( $contin ); }
        Specifically I think you're running into a problem at
        if ( $contractors =~ m/^$string(.*)/ )
        It looks like what you want to be doing is matching every line in $contin against your current $contractor where what you're actually doing is matching $contractor against your regex ^$string(.*) Try changing the above regex in the while loop to have something like
        while ( <$contin> ) { if ($_ =~ m/^$contractor(.*)/) { my @results = split(m#\s+#, $1); print br; print @results; } } close( $contin ); }
Re: Using elements to search a file
by Narveson (Chaplain) on May 06, 2009 at 16:23 UTC

    What happened when you ran this code?

      The part I'm having issues with doesn't return anything at all.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-12-07 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (33 votes). Check out past polls.

    Notices?