[英]How do I multiply each member of array by a scalar in perl?
Here is the code...这是代码...
use strict;
use warnings;
my @array= (1,2,3,4,5);
my $scalar= 5;
@array= $scalar*@array;
print @array;
Need something that can perform similar function with little code.需要一些可以用很少的代码执行类似功能的东西。 Thanks!谢谢!
使用 foreach。
foreach my $x (@array) { $x = $x * $scalar; }
You can try this:你可以试试这个:
@array = map { $_ * $scalar } @array;
or more simply:或更简单地说:
map { $_ *= $scalar } @array;
Howabout this:这个怎么样:
foreach(@array)
{ $_ *= $scalar }
As you see, you can modify the array in-place as it's traversed.如您所见,您可以在遍历数组时就地修改它。
I don't know the scope of your need.我不知道你的需求范围。 IFF you are doing numerical data manipulation, the Perl Data Language ( PDL ) takes an array of numerical data, creates a "piddle" object from it and overloads mathematical operations to "vectorize" their operation.如果您正在进行数值数据操作,Perl 数据语言 ( PDL ) 会采用一组数值数据,从中创建一个“piddle”对象并重载数学运算以“矢量化”它们的运算。 This is a very efficient system for doing numerical processing.这是进行数值处理的非常有效的系统。 Anyway here is an example:无论如何,这是一个例子:
#!/usr/bin/perl
use strict;
use warnings;
use PDL;
my $pdl_array = pdl([1,1,2,3,5,8]);
print 2*$pdl_array;
__END__
gives:
[2 2 4 6 10 16]
This comment is for SoloBold.此评论适用于 SoloBold。
Here is a test of the map
approach:这是map
方法的测试:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;
my $startTime = new Benchmark();
@array = map { $_ * $scalar } @array;
my $stopTime = new Benchmark();
print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";
Here is a test of the foreach
approach:这是对foreach
方法的测试:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;
my $startTime = new Benchmark();
foreach my $x (@array) { $x = $x * $scalar; }
my $stopTime = new Benchmark();
print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";
Here is the system I'm running on:这是我正在运行的系统:
bash-3.2$ perl --version
This is perl, v5.8.8 built for darwin-2level
...
bash-3.2$ uname -a
Darwin Sounder.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386
Here were results from one test:以下是一项测试的结果:
bash-3.2$ ./test.map.pl
runtime: 4 wallclock secs ( 0.41 usr 0.70 sys + 0.00 cusr 0.00 csys = 1.11 CPU) sec
bash-3.2$ ./test.foreach.pl
runtime: 0 wallclock secs ( 0.13 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.13 CPU) sec
These times are fairly reproducible on the same machine, and the results are somewhat repeatable on a dual-core Linux box:这些时间在同一台机器上相当可重现,结果在双核 Linux 机器上有些可重复:
[areynolds@fiddlehead ~]$ perl --version
This is perl, v5.8.8 built for x86_64-linux-thread-multi
...
[areynolds@fiddlehead ~]$ uname -a
Linux fiddlehead.example.com 2.6.18-194.17.1.el5 #1 SMP Mon Sep 20 07:12:06 EDT 2010 x86_64 GNU/Linux
[areynolds@fiddlehead ~]$ ./test.map.pl
runtime: 0 wallclock secs ( 0.28 usr 0.05 sys + 0.00 cusr 0.00 csys = 0.33 CPU) sec
[areynolds@fiddlehead ~]$ ./test.foreach.pl
runtime: 0 wallclock secs ( 0.09 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.09 CPU) sec
The ratio of performance on the OS X box is 8.53x slower for map
versus foreach
. OS X box 上的性能比对于map
比foreach
慢 8.53 倍。 On the Linux box, 3.67x slower for the same.在 Linux 机器上,同样慢 3.67 倍。
My Linux box is dual-core and has a slightly faster cores than my single-core OS X laptop.我的 Linux 机器是双核的,比我的单核 OS X 笔记本电脑的内核要快一些。
EDIT编辑
I updated Perl from v5.8.8 to v5.12.3 on my OS X box and got a considerable speed boost, but map
still performed worse than foreach
:我在我的 OS X 机器上将 Perl 从 v5.8.8 更新到 v5.12.3 并获得了相当大的速度提升,但map
仍然比foreach
差:
sounder:~ alexreynolds$ perl --version
This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-multi-2level
...
sounder:~ alexreynolds$ ./test.map.pl
runtime: 0 wallclock secs ( 0.45 usr 0.08 sys + 0.00 cusr 0.00 csys = 0.53 CPU) sec
sounder:~ alexreynolds$ ./test.foreach.pl
runtime: 1 wallclock secs ( 0.18 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.18 CPU) sec
This goes from 8.53x worse to 2.94x worse.这从糟糕的 8.53 倍变为糟糕的 2.94 倍。 A fairly substantial improvement.相当可观的改进。
The Linux box performed slightly worse with upgrading its Perl installation to v5.12.2:在Linux系统中,其Perl安装升级到v5.12.2略差进行:
[areynolds@basquiat bin]$ perl --version
This is perl 5, version 12, subversion 2 (v5.12.2) built for x86_64-linux-thread-multi
...
[areynolds@basquiat bin]$ /home/areynolds/test.map.pl
runtime: 1 wallclock secs ( 0.29 usr 0.07 sys + 0.00 cusr 0.00 csys = 0.36 CPU) sec
[areynolds@basquiat bin]$ /home/areynolds/test.foreach.pl
runtime: 0 wallclock secs ( 0.08 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.08 CPU) sec
This goes from 3.67x worse to 4.5x worse — not so good!这从 3.67 倍差到 4.5 倍差——不太好! It might not always pay to upgrade, just for the heck of it.升级可能并不总是付费,只是为了它。
it seem unfortunate to me that Larry didn't allow我觉得很不幸,拉里不允许
$scalar operator (list) $标量运算符(列表)
or或者
(list) operator $scalar (列表)运算符$scalar
Sure map or loops can do it, but the syntax is so much cleaner like above.当然 map 或 loops 可以做到,但语法比上面的要简洁得多。
Also (list) operator (list)也(列表)运算符(列表)
makes sense too if the 2 are equal length.如果 2 的长度相等,这也很有意义。
Surprised Larry didn't allow these, just saying.. I guess in this case there were (n-1) ways to do it.惊讶的拉里不允许这些,只是说.. 我想在这种情况下有 (n-1) 种方法可以做到。
Like喜欢
my @a = 'n' .我的 @a = 'n' 。 (1..5); (1..5); my @a = 2 * (1..5);我的@a = 2 * (1..5);
or even my @a = 2 * @b;甚至我的@a = 2 * @b;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.