Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: cool way of manipulating array elements

by BioLion (Curate)
on Aug 30, 2011 at 08:02 UTC ( #923139=note: print w/replies, xml ) Need Help??

in reply to cool way of manipulating array elements

Maybe map? and a util function to test for whether each element is a number - Scalar::Util?

use strict; use warnings; # or some other util function that can tell if it is a number... use Scalar::Util qw(looks_like_number); my @array = ('1','2','45','65','what is this?'); my @mod = map {if (looks_like_number($_)){$_*2} else {$_}} @array; print "$_\n" for @mod;

Hope this helps... (I am a bit Rusty, so be gentle...)

Just a something something...

Replies are listed 'Best First'.
Re^2: cool way of manipulating array elements
by pashanoid (Scribe) on Aug 30, 2011 at 08:18 UTC
    thank you! works great!

      If you want to modify the original array contents then you could modify BioLion's code thus:

      $_ *= 2 for grep {looks_like_number($_)} @array;
      True laziness is hard work

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-09-17 20:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found