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

I have this bit of cgi which is meant to replace occurences of 'FLF' followed by two digits (in an html file) with the appropriate fields from an SQL query result ie I get the field contents into an array element then parse the html file to find "FLF/d/d" using a substitution that is the SQL fetch is like so
while($row = $sth->fetchrow_arrayref) { $Replacement[12]=$row->[0]; #etc.... #then in the loop still... $Kine=~s/FLF(\d\d)/$Replacement[$1]/ge;
I thought this would replace occurences of 'FLF' followed by two digits with the value of the array element with that index, ie FLF12 becomes value of $Replacement[12] , however it does this but only uses the first value of $row-> [0] and doesn't provide the other row data for all the other rows following, but if you print the value of $row->[0] at that point in the code it is fine (ie contains the data from each successive row no problem?
Hope this is understandable!!
thanks to Stephen and wmono for suggesting using this form.
splendid...
catcher

Edit by dws to close <code> tag and remove entities

Replies are listed 'Best First'.
Re: Perl SQL substitution pain!
by stephen (Priest) on Apr 08, 2002 at 00:24 UTC
    I could be misunderstanding, but if I'm reading you right, the code is something like this:
    use strict; my $Kine = "Hello, FLF12. Would you like to play FLF22?"; # I've substituted array refs for the SQL query. It's # approximately the same as the 'while' line you have. foreach my $row (["Dr. Falken", "a game"], ["Bobby Fischer", "some che +ss"]) { my @Replacement = (); $Replacement[12]=$row->[0]; $Replacement[22]=$row->[1]; $Kine=~s/FLF(\d\d)/$Replacement[$1]/ge; print $Kine, "\n"; }
    Here's my guess (and it's just a guess) as to what's going on: you're doing a substitution for each row that you pull out of the database. First row, everything substitutes fine, so $Kine is 'Hello, Dr. Falken. Would you like to play a game?' Then we get to the second row. The regexp scans $Kine again, but this time it contains no tags. In fact, it contains 'Hello, Dr. Falken. Would you like to play a game?' still. So there are no tags for it to replace, and so it prints out 'Hello, Dr. Falken. Would you like to play a game?' again.

    Instead, try this:

    use strict; my $Kine = "Hello, FLF12. Would you like to play FLF22?"; foreach my $row (["Dr. Falken", "a game"], ["Bobby Fischer", "some che +ss"]) { my @Replacement = (); $Replacement[12]=$row->[0]; $Replacement[22]=$row->[1]; # Copy $Kine into temporary variable my $out = $Kine; $out =~ s/FLF(\d\d)/$Replacement[$1]/ge; print $out, "\n"; }
    Which prints out:
    Hello, Dr. Falken. Would you like to play a game? Hello, Bobby Fischer. Would you like to play some chess?

    stephen

Re: Perl SQL substitution pain!
by JayBonci (Curate) on Apr 08, 2002 at 09:32 UTC
    I could be totally wrong, but are you sure you want to replace ALL of them in the first loop? Like here is what I'm seeing as you're going through.
    while($row = $sth->fetchrow_arrayref) { #pick up the first row as an array ref $Replacement[12]=$row->[0]; #at this point you have one item from the sql query in your #Replacement array $Kine=~s/FLF(\d\d)/$Replacement[$1]/ge; #replace all of them items in $Kine

    Because you are using the /g qualifier at the end of your regular expression, it's going to hit absolutely everything in the loop, regardless of whether the SQL loads it first or not. There isn't any qualifier on the regex as to what to match, except _any_ two digits. After that point all of your FLF's have been transformed, so it's done matching. You may consider restructuring your loop or your regular expression to more closely match what you receive in $row->[0], which ever you feel more comfortable doing.

    The previous example works only because they start with all of the data going into the regex, where as you do not.

    Hope this helps.

        --jb