In article <cel5b7$66f$>,
(Anno Siegel) wrote:
> > Hello again, and thanks to Anno Siegel for your answer to my earlier thread
> > (news:mr-) on the subject, but
> > unfortunately, the conditions for my need has slightly altered, and I want
> > some
> > further help (from anyone, not just Anno).
> >
> >
> > Here is a code snippet that probably illustrates what I want to do:
> >
> > -------------
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
> >
> > my $string = "Hello World!";
> >
> > my %array = (
> > "Hello (.*?)!", "Goodbye, little #1#!"
> > # search reply
> > );
> > foreach (keys %array){
> > if ($string=~m/$_/i){
> > # It matched!
> >
> > $array{$_}=~s/#(\d+)#/$$1/;
>
> Well, this doesn't do what you hope it does. You knew that, and I
> showed you a way to do the replacements. Why are you trying it again?
I'm not, this is just an illustrative script to givbe the gist of what I want
to do, and while your script worked for the purpose of my earlier needs, it
doesn't for these altered needs, so I went back to this script which doesn't
work, but should give the reader an idea of what I want to achieve.
Plus, your script replaced strings, which I no longer wish to do.
> > # replace #1# with $1, #2# with $2
> > # from the match above
> >
> > print "$string\n\n";
> > print "$array{$_}\n\n";
> > # Add the reply to the string
>
> What does that mean? Append it?
Bad wording. The script should output the original string, with the "reply"
printed after it, with #1#, #2#... expanded into the matched values from the
search string.
> > }
> > }
> > -------------
> >
> > Wanted output:
> >
> > Hello World!
> >
> > Goodbye, little World!
> >
> > So, as opposed to my earlier thread, I no longer seek to replace the
> > original string, but rather add a "reply" to it, if it matches.
>
> Trivial. Copy the original, modify the copy, concatenate both.
>
> > Basically, what I
> > want
> > to do is replace the string #1#, #2#, #3#... with the values in $1, $2,
> > $3...
> > but when trying to do that, perl complains about:
> >
> > File "test.pl"; Line 15: Can't use string ("1") as a SCALAR ref while
> > "strict refs" in use
> >
> > Line 15 being:
> >
> > $array{$_}=~s/#(\d+)#/$$1/;
>
> I've shown a way to do that in my earlier reply. Your changed
> requirements have nothing to do with how to achieve this, you'd have
> to do it in any case. So, apply my solution (or another one) to the
> new situation. Don't go back to square one, just because an irrelevant
> detail has changed.
Well, my problem with your earlier script was this (note the altered string):
#!/usr/bin/perl
use strict;
use warnings;
my %tab = (
'Hello (.*?)!' => 'Goodbye, little #1#...',
'Welcome (.*?) and (.*?)!' => 'Farewell, honorable #2# and fair #1#!',
);
for ( "Welcome ladies and gentlemen! Please step inside", "Hello World!" ) {
my $string = $_; # make a copy we can change
while ( my ( $search, $replace) = each %tab ) {
# build ready-to-use replacement string from template
my $n = 1; # submatch number
for ( $string =~ /$search/ ) { # loop over submatches
$replace =~ s/#$n#/$_/g, # substitute one submatch everywhere
$n ++; # next submatch
}
last if $string =~ s/$search/$replace/;
}
print "$string\n";
}
This outputs
Farewell, honorable gentlemen and fair ladies! Please step inside
Goodbye, little World...
Which is undesirable. And, oddly enough, it only worked occasionally with one
specific string (i.e. running the script five times in a row made it match it
two times, highly bizarre).
So, despite your efforts, I felt I wanted to go back to square one and attack
the faulty line in my little script.
Isn't there a way, while using strict refs, to replace #1#, #2#, #3#... with
the values of $1, $2, $3, which are created by a match operation?
--
Sandman[.net]