in reply to Re: Converting quoted strings to numbers in array
in thread Converting quoted strings to numbers in array

Hi Choroba,

Thank you for replying. Please let me know if there is a reason to use a oct function for a hex string?

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

Replies are listed 'Best First'.
Re^3: Converting quoted strings to numbers in array
by kcott (Archbishop) on Apr 24, 2017 at 05:53 UTC

    If you were dealing with a mixture of binary (as 0b...), hexadecimal (as 0x...), and octal strings, e.g. in a loop, then oct would be useful.

    When you know it's a hexadecimal string (e.g. /^0x/ && ...), I'd use the hex function. It makes your intent clear and obvious and, in my opinion, the code is far more readable.

    The actual result is the same whichever you use:

    $ perl -E 'say oct "0x47"; say hex "0x47"' 71 71

    And just a word of warning, /^0x/ may not do what you want:

    $ perl -E 'say "0x47_not_hex" =~ /^0x/ ? "is hex" : "is NOT hex"' is hex $ perl -E 'say "0x47_not_hex" =~ /^0x[0-9a-fA-F]+$/ ? "is hex" : "is N +OT hex"' is NOT hex

    — Ken

Re^3: Converting quoted strings to numbers in array
by choroba (Cardinal) on Apr 23, 2017 at 21:15 UTC
    The word oct in my reply was a link. Have you clicked it and read the documentation?

    > If EXPR happens to start off with 0x , interprets it as a hex string.

    Maybe not the best name for such a function, but that's how it is.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,