Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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...


Comment on Re: cool way of manipulating array elements
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-08-02 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls