> hi
> eh, how do i in fact use variables that really are variable in a regexp?
Not sure exactly what you mean here, because variables *really are* variable
in a regex.
> in the below script i just want to get this out:
> first value: 33 sec value: 44
> however i get:
> first value: 33 sec value: <xml2>first value: <xml1> sec value: 44
The code you provided replaces "<xml1>" with "33" and "<xml2>" with "44" in
separate calls to perlFunction(), so the replacements aren't occurring at
the same time. You're expecting perlFunction() to replace each key of %hash
with its respective value, but you're only passing a single key/value pair
to perlFunction().
Try this instead:
#/usr/bin/perl -w
%hash = ("<xml1>", 33, "<xml2>", 44);
my $string = "first value: <xml1> sec value: <xml2>";
print perlFunction($string, \%hash);
#should output first value: 33 sec value: 44
sub perlFunction() {
my($str, $hashref) = @_;
while(my($key, $val) = each %$hashref) {
$str =~ s/$key/$val/gi;
}
return $str;
}
I know there are some cleaner ways to do this, but they slip my mind for the
moment... :-)
HTH
Regards,
David Iberri
> i dont see another way right now than calling a function with the
> variables, but then i have a problem everytime i call the function. in
> this case it prints the return string twice. i know about the /o
> modifier so i guess this is possible and probably quite easy:-)
>
> thanks
> allan
>
>
> #!/usr/bin/perl -w
>
> %hash = ("<xml1>", 33, "<xml2>", 44);
>
> $string = "first value: <xml1> sec value: <xml2>";
>
> while (($key,$value) = each %hash) {
> $output = perlFunction($string, $key, $value);
> print $output;
> }
>
> #should output first value: 33 sec value: 44
>
> sub perlFunction() {
> $strFromAsp = shift(@_);
> $perl_inputstring = $strFromAsp;
> $perl_inputstring =~ s/$key/$value/i;
> return $perl_inputstring;
> }