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 |