in reply to parsing and evaluating a string

Or:
use strict; use warnings; my $str = 'char(123)+char(107)+char(112)+char(43)'; $str =~ tr/+/ /; $str =~ s/char\((\d+)\)/chr($1)/eg; print $str;
I know I could have made this neater by using $_, but he probably has the value in some variable.

Replies are listed 'Best First'.
Re^2: parsing and evaluating a string
by johngg (Canon) on Apr 24, 2006 at 13:11 UTC
    The down side to your solution is that it introduces spaces (because of the tr/+/ /) into the string that weren't in the original chr(n1)+chr(n2)+... sequence. The solution is to change the substiution

    $str =~ s/char\((\d+)\) ?/chr($1)/eg;

    Cheers,

    JohnGG