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.

Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!

In reply to Converting quoted strings to numbers in array by pritesh_ugrankar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.