Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks
I'm having some trouble in printing a hash value (normal integer) as a hexadecimal value. This value has to be small caps, and 2 digits long
When I do a simple script like this to test the code:
my %hash; $hash{a}{key} = 7; $hash{b}{key} = 10; $hash{c}{key} = 11; $hash{d}{key} = 12; $hash{e}{key} = 13; foreach my $x (keys %hash) { printf "%02x\n", $hash{$x}{key}; }
-it works perfectly fine
In the example code I try to print a hash value(which is a normal integer) as a hex value. I am using the printf and hex functions, but somehow I get stuck. For instance if my hash value is 10, I want the output to be 0a. All I get back though is normal ints.
Thanks in advance for any help, much appreciated
Update
But when I try to implement that in my real script, it does not work
foreach my $id (sort keys %hash) { printf Out "%02x\n", $hash{$id}{"counter"}; }
Somehow it filters out all values larger than 10 from my output. This is strange because the code is the same...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing a int as hex
by toolic (Bishop) on Sep 09, 2013 at 19:00 UTC | |
|
Re: Printing a int as hex
by McA (Priest) on Sep 09, 2013 at 18:59 UTC | |
|
Re: Printing a int as hex
by Laurent_R (Canon) on Sep 09, 2013 at 21:14 UTC | |
|
Re: Printing a int as hex
by marinersk (Priest) on Sep 09, 2013 at 22:17 UTC |