in reply to cool way of manipulating array elements

print map {( $_ =~ /\d/ ? $_ * 2 : '-' ),"\n"} @array;

Maybe there is other way to do this but this is one ...

Replies are listed 'Best First'.
Re^2: cool way of manipulating array elements
by norbert.csongradi (Beadle) on Aug 30, 2011 at 09:02 UTC

    Be careful with such a "lazy" regexp, it might give you funny results (looks_like_number is smarter ;):

    @array = ('abc2', 'hehe42'); # not really numbers, but passes regexp ; +)
Re^2: cool way of manipulating array elements
by Kc12349 (Monk) on Aug 31, 2011 at 16:58 UTC
    print map {( $_ =~ /^-?\d+$/ ? $_ * 2 : '-' ),"\n"} @array;

    You could try something like this to catch integers.

    print map {( $_ =~ /^-?(\d*\.)?\d+$/ ? $_ * 2 : '-' ),"\n"} @array;

    Or something like this to catch numbers with decimals. Both of these will simply ignore leading zeros as perl is apt to do when converting strings to numbers.