Hi,
I'm trying to crack this problem from a while now, but do not seem to find a way through it. I'm sure something obvious is being missed. I was asked to take a shot at this while requesting review on an earlier post here: Re^4: add missing elements and remove duplicates....and some more questions..
I am not looking for a direct answer though, I just need some pointers.
I have an array like this:my @arr= qw (10 10 20 0x47 1 30 45 45);
I am trying to convert it to an array with numbers,but the quotes seem to cause problems. So I tried this:
use strict; use warnings; use v5.10.1; my @arr= qw (10 10 20 0x47 1 30 45 45); my @converted = grep $_ !~ /'/, @arr; foreach my $element (@converted) { my $num = sprintf "%d", $element; print "$num\n"; }
But I still get this:
10 10 20 Argument "0x47" isn't numeric in sprintf at num.pl line 8. 0 1 30 45 45
However, if I just try the hex number by itself, that seems to work:
use strict; use warnings; use v5.10.1; my $hexnum = sprintf ("%d", 0x47); print "$hexnum\n";
The above prints 71 which is the decimal for hex 0x47. What am I missing here? Please help.
In reply to Converting quoted strings to numbers in array by pritesh_ugrankar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |