in reply to parsing and evaluating a string

This does what I think you're asking, but without context I can't give you anything more.

use strict; use warnings; my $string = 'char(123)+char(107)+char(112)+char(43)'; # change all 'char' to 'chr' (the Perl function) $string =~ s/char/chr/g; # split the string on '+', eval each part, then join the resulting # characters back into a single string $string = join( '', map { eval $_ } split( /\+/, $string ) ); print "$string"; # {kp+

Please note that this is very simple code that makes a lot of assumptions. For example, will all of your strings be of the format described in the OP? Will there ever be any other functions besides chr? The eval could be risky - how much do you trust the source of the string?

HTH

Replies are listed 'Best First'.
Re^2: parsing and evaluating a string
by bfdi533 (Friar) on Apr 24, 2006 at 12:56 UTC

    Your assumtions are rather correct.

    All strings will be in this format except that they will have stuff before them and stuff behind them. This is actually embedded into a SQL select statement and needs to be evaluated to be able be read and/or parsed properly. For instance:

    select char(123)+char(107)+char(112)+char(43) from table

    No, there will not be any other functions and it can most definitely be trusted.