in reply to Escaping a variable

my $foo = "4D"; my $bar = "\x4D"; print $bar . "\n";

Doesn't produce

Illegal hexadecimal digit '$' ignored at roman.pl line 10. 4D.

Why don't you please edit your post and include the actual code that is giving you that error message? I suspect I know what it looks like, but it's your job to actually show code that exhibits the problem you're describing.

At any rate, hex will, given the string contained in $foo as a parameter, return the base ten integer value 77.


Dave

Replies are listed 'Best First'.
Re^2: Escaping a variable
by Draxter (Initiate) on Nov 08, 2011 at 09:19 UTC

    Hi,

    Sorry, you are right of course.

    This is what I do:

    my $foo = "4D"; my $bar = "\x$foo"; print $bar . "\n"; Illegal hexadecimal digit '$' ignored at roman.pl line 7. 4D

      print hex($foo), "\n";

      The output:

      77

      Dave

        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.

        I don't want the hex value of the variable, I need the variable to be printed as an ascii representation of the hex value.