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

I am new to coding in Perl. I’m using for each and while code to read in a string from a file and isolate a 4 digit number in that string and use it in a sql query. I bring back the results of the query and append the results of the query to that string I read and got the number from.

My problem is that it performs everything I want one time, but repeats the same query results for the remaining strings that are written. It should have different query results b/c the number in the next string that is read will be different. My dB connect and sql code works fine. The trouble, I think, is here in looping the read/append of string + query.
Open $fh for reading Open $fh3 for writing While (my $var = <$fh>) { Chomp $var; my @array1 = $var =~ (/Entry\s*(\d*)/); foreach my $e (@array1) } &querysub; Open $fh for reading While (<$fh>) } if ($_ =~ /(.*Entry\s*\d*)(.*)/); print $fh3 $1.” “.$qresult.$2.”\n”; } } }

Replies are listed 'Best First'.
Re: Reading and writing back an updated string
by roboticus (Chancellor) on Jun 26, 2019 at 20:35 UTC

    mgwump:

    Perhaps you should try writing perl code instead of whatever that is.

    $ perl -c tmpx.pl "my" variable $var masks earlier declaration in same statement at tmpx +.pl line 5. Global symbol "$fh" requires explicit package name (did you forget to +declare "my $fh"?) at tmpx.pl line 1. Global symbol "$fh3" requires explicit package name (did you forget to + declare "my $fh3"?) at tmpx.pl line 2. syntax error at tmpx.pl line 2, near "$fh3 for " Global symbol "$fh" requires explicit package name (did you forget to +declare "my $fh"?) at tmpx.pl line 4. Can't redeclare "my" in "my" at tmpx.pl line 6, near "my" Global symbol "$var" requires explicit package name (did you forget to + declare "my $var"?) at tmpx.pl line 6. syntax error at tmpx.pl line 7, near ") }" Global symbol "$fh" requires explicit package name (did you forget to +declare "my $fh"?) at tmpx.pl line 9. Global symbol "$fh" requires explicit package name (did you forget to +declare "my $fh"?) at tmpx.pl line 10. Unmatched right curly bracket at tmpx.pl line 10, at end of line tmpx.pl has too many errors.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Reading and writing back an updated string
by poj (Abbot) on Jun 26, 2019 at 20:56 UTC

    Open the input file once and use a single while loop.
    Pass the number to the querysub as a parameter and return the result.

    #!/usr/bin/perl use strict; open my $fh_in,'<','input.txt' or die "Could not open input $!"; open my $fh_out,'>','output.txt' or die "Could not open output $!"; while (my $var = <$fh_in>){ if ( $var =~ /(.*Entry\s*)(\d*)(.*)/ ){ my $qresult = querysub($2); print $fh_out $1.$2.$qresult.$3."\n"; } } close $fh_in; close $fh_out; sub querysub { my ($num) = @_; return "[Result with $num]"; # test } #input.txt # Entry 1 abc # Entry 2 def # entry 3 hij
    poj