Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
No such thing as a small change
 
PerlMonks  

Re: Perl SQL substitution pain!

by stephen (Priest)
on Apr 08, 2002 at 00:24 UTC ( #157341=note: print w/ replies, xml ) Need Help??


in reply to Perl SQL substitution pain!

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


Comment on Re: Perl SQL substitution pain!
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://157341]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-04-23 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (557 votes), past polls