Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Escaping a variable

by Draxter (Initiate)
on Nov 08, 2011 at 09:19 UTC ( [id://936695]=note: print w/replies, xml ) Need Help??


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

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

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

    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.

        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

Re^3: Escaping a variable
by moritz (Cardinal) on Nov 08, 2011 at 09:24 UTC

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://936695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found