Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Converting quoted strings to numbers in array

by Anonymous Monk
on Apr 23, 2017 at 21:17 UTC ( [id://1188723]=note: print w/replies, xml ) Need Help??


in reply to Converting quoted strings to numbers in array

my @arr = ( 1, 2, 3, 0x47 ); are numeric literals, see Scalar value constructors. In the source code they can look like 12345 or 4_294_967_296 or 0xff or 0377 or 0b011011 and will all be converted to numbers and stored as numbers. my @arr = qw/ 1 2 3 0x47 /; are strings, see qw//, it's the same as writing my @arr = ( "1", "2", "3", "0x47" );. In the source code, 0x47 is different from "0x47", the first one means 71 and the second one is a string with four characters. Strings will only be automatically converted to numbers if they are plain integers. If you want to know if a string will automatically convert to a number, you can use looks_like_number from Scalar::Util. "0x47" is not a plain integer, it's got an x character. Use oct to convert that string to a number, oct it does octal, 0x123 and 0b101 formats, or use hex which only does hexadecimal.

Replies are listed 'Best First'.
Re^2: Converting quoted strings to numbers in array
by Anonymous Monk on Apr 23, 2017 at 22:53 UTC
    Strings will only be automatically converted to numbers if they are plain integers.

    Or decimals, or scientific notation, or "Inf". But not hex or 12_34 notation, or 0b..., and octal numbers (0123 == 83) won't be recognized as such ("0123" == 123).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found