in reply to Re^3: Escaping a variable
in thread Escaping a variable

I'll be more specific. How can I get $string and $bar to be the same by the end of this code:

#!/usr/bin/perl use warnings; use strict; my $bar = "\x04\x4d"; my $foo = "044d"; my $string; while ($foo =~ m/(..)/g) { $string .= chr($1); } print "string: $string\n"; print "bar: $bar\n"; Argument "4d" isn't numeric in chr at roman.pl line 11. string:  bar: M

Thanks,

Eran.

Replies are listed 'Best First'.
Re^5: Escaping a variable
by davido (Cardinal) on Nov 08, 2011 at 09:40 UTC

    It's easier when you read the directions we pointed to: hex, chr.

    use warnings; use strict; use Test::More; my $bar = "\x04\x4d"; my $foo = "044d"; my $string; while ($foo =~ m/(..)/g) { $string .= chr(hex($1)); } is( $bar, $string, '$bar eq $string' ); done_testing(); print "string: $string\n"; print "bar: $bar\n"; __END__ ok 1 - $bar eq $string 1..1 string: M bar: M

    Update: For the record, Test::More, is(), and done_testing() are not significant to the solution. I used them to demonstrate the equality.


    Dave