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

mutliplying each element of a list by the same number

by steph_bow (Pilgrim)
on May 15, 2008 at 13:51 UTC ( [id://686727]=perlquestion: print w/replies, xml ) Need Help??

steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I would like to know if there is a simple way to multiply each element of a list by the same number. I would like to do, for example : 10* (1..10);

use strict; my $max = $ARGV[0]; my @ratio_list = (0,1..$max); my @minutes_list; foreach my $k(@ratio_list){ my $inter = 10 * $ratio_list[$k]; if ($inter != 0){ push @minutes_list, $inter; } }

Thanks a lot !

Replies are listed 'Best First'.
Re: mutliplying each element of a list by the same number
by Fletch (Bishop) on May 15, 2008 at 14:00 UTC

    map

    my @minutes_list = grep $_ != 0, map $_ * 10, @ratio_list;

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      or

      my @minutes_list = map $_*10 || (), @ratio_list;

      (There's nothing at all wrong with your solution. I just wanted to point out an often overlooked feature of map ... that is, it can return a list shorter than the input list, if you have the expression evaluate to the empty list for the elements to be dropped.)

Re: mutliplying each element of a list by the same number
by toolic (Bishop) on May 15, 2008 at 14:05 UTC
    You can 1st use grep to remove the unwanted "0" from your list, then use map to transform the remaining elements of the list (i.e., multiplying by 10):
    use strict; use warnings; my $max = $ARGV[0]; my @ratio_list = (0,1..$max); my @minutes_list = map {$_ * 10} grep {$_ != 0} @ratio_list; print "@minutes_list\n";

    prints:

    10 20 30 40 50
Re: mutliplying each element of a list by the same number
by moritz (Cardinal) on May 15, 2008 at 14:39 UTC
    You've got plenty of Perl 5 answers, let me add a Perl 6 one:
    pugs> my @nums = 1, 2, 4, 5, 0, 8; pugs> grep { $_ }, 10 >>*<< @nums; (10, 20, 40, 50, 80)

    (I think that standard perl 6 would use 10 * << @nums instead, only the side where a dimension is folded gets the hyper op. But pugs is a bit out of date).

    Likewise you can add or multiply arrays piecewise:

    pugs> <1 2 3> »+« <0 1 0> (1.0, 3.0, 3.0)

    ( » is an alias for >>, « an alias for <<)

    And, very powerful as well, the reduce operator:

    pugs> [+] <1 2 3> 6.0
Re: mutliplying each element of a list by the same number
by johngg (Canon) on May 15, 2008 at 14:07 UTC
    You could use grep and map.

    use strict; use warnings; my $max = shift; my @rl = ( 0 .. $max ); my @ml = map { $_ * 10 } grep { $_ } @rl; print qq{$_\n} for @ml;

    Cheers,

    JohnGG

Re: mutliplying each element of a list by the same number
by moklevat (Priest) on May 15, 2008 at 14:34 UTC
    PDL makes this operation trivial.
    #!/usr/bin/perl use strict; use warnings; use PDL; my $max = $ARGV[0]; my $ratio_list = pdl (0..$max); my $minutes_list = 10 * $ratio_list;
      perl -MPDL -e '$n=shift; print +(sequence($n)+1)*$n'

      Turning the resulting ndarray into a flattened Perl list (with the list method) or structured Perl array (with the unpdl method) is left as an exercise for the reader.

      Kind-of seems to me like pounding a thumbtack with a sledgehammer. Of course, TIMTOWTDI

      --
      Wade
        Sure, but steph_bow asked for simple. It was simple to write and it's simple to read and it DWIM.
Re: mutliplying each element of a list by the same number
by olus (Curate) on May 15, 2008 at 14:06 UTC
    for (0..$#ratio_list){ $ratio_list[$_] *= 10; }

    This does not filter values that are 0, but does multiply every element of the array by 10. Not sure if you really want to filter zeroes.

      This is just slightly more efficient (at least in terms of keystrokes if not execution time):
      for (@ratio_list) { $_ *= 10 }
      If you wanted to simultaneously create @minutes_list, you can use:
      for (@ratio_list) { ($_ *= 10) && push(@minutes_list, $_) }
        for (@ratio_list) { $_ *= 10 }

        If you're going to do that, why not get rid of useless punctuation altogether?

        $_ *= 10 for @ratio_list;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://686727]
Approved by olus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found