in reply to Converting quoted strings to numbers in array

Hi,

Thank you for taking time to reply to my query. Using the suggestions here, I have come up with the following solution.

use strict; use warnings; my @arr = qw(10 20 0b1101 0xF 023); my @converted = map {$_ =~ /^0/ ? oct($_) : $_ } @arr; print "@arr\n"; print "@converted\n";

The output seems satisfactory:

C:\>perl practice.pl 10 20 0b1101 0xF 023 10 20 13 15 19

I'm not sure if using a ternary operator inside map is considered un idiomatic, but it seems to work.

Thank you all, thank you very much. This I know would seem trivial to you, but it's fixing something I've been fighting in my head from a while.

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

Replies are listed 'Best First'.
Re^2: Converting quoted strings to numbers in array
by haukex (Archbishop) on Apr 24, 2017 at 13:01 UTC
    I'm not sure if using a ternary operator inside map is considered un idiomatic, but it seems to work.

    I'd say it's idiomatic, and can even be shortened since $_ is used as the default argument for many functions (including oct) and for regexes that aren't bound to something via =~:

    use Data::Dump; my @arr = qw/ 10 20 0b1101 0xF 023 /; dd @arr; my @converted = map {/^0/ ? oct : $_} @arr; dd @converted; __END__ (10, 20, "0b1101", "0xF", "023") (10, 20, 13, 15, 19)