Peter Daum am Freitag, 3. November 2006 20:26:
Hoi Peter,
> I am looking for an way to interpolate backslash-sequences
> within a string with the usual perl semantics, e.g.
> $s='1\t\2\t3\na\ b c' should become:
> '1<tab>2<tab>3
> a b c'
With usual perl semantics, the result is different :-)
> Things I tried were for example
> $s= eval('' . "$val"); # (hoping to trigger the normal interpolation) or
> $s=~ s/\\(.)/"\\$1"/eg;
> but somehow i couldn't get it right ...
>
> Can anybody think of an elegant solution?
I think there are more elegant solutions, but in the following you have full
control over what translates to what:
#!/usr/bin/perl
use strict;
use warnings;
my %trans=(
'\t'=>"\t",
'\n'=>"\n",
'\ '=>' ',
'\2'=>'2',
q(\\)=>qw( \ ), # ;-)
);
my $s='1\t\2\t3\na\ b c \\\ '; # last space: ;-)
$s=~s; (\\.) ; $trans{$1} || $1 ;gex;
print "<$s>\n";
# Note "the usual perl semantics":
print "<\2><\ >\n";
__END__
Dani
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>