Perl Map函数无法按预期工作

时间:2012-08-08 22:00:36

标签: arrays perl map

我需要一些关于Perl中map函数的帮助,它似乎可以用来组合我的数组。

#!/usr/bin/perl

use Math::Trig;

my @Degre = map {rand(360)} (1..2000);
my @step= map {rand(.5)} (1..2000);
my @aa = map {rand(2000)} (1..2000);
my @bb = map {rand(2000)} (1..2000);

for ($i = 0; $i <=100; $i++)

{
my @xx = map {$aa[$_]*(cos($Degre[$_])*(pi/180))}(1..2000);
my @yy = map {$bb[$_]*(cos($Degre[$_])*(pi/180))}(1..2000);

@Degre = map {@Degre[$_] + @step[$_]} (1..2000);


print "@bb[1]  @aa[1]  @Degre[1] @step[1] \n";
}

现在这个出来了

1146.56471948439  1909.33326800968  329.443529905881 0.117635819122725
1146.56471948439  1909.33326800968  343.482356802257 0.117635819122725
1146.56471948439  1909.33326800968  164.500200570578 0.117635819122725
1146.56471948439  1909.33326800968  252.734665366625 0.117635819122725
1146.56471948439  1909.33326800968  274.983382178209 0.117635819122725
1146.56471948439  1909.33326800968  324.609187610893 0.117635819122725
1146.56471948439  1909.33326800968  261.96207333817 0.117635819122725
1146.56471948439  1909.33326800968  279.442105351764 0.117635819122725

第3列是度数,我不明白为什么它似乎随机跳转,当我预计它会增加0.117635 .....步骤?

干杯

更新

确认我正在尝试获取以下

的地图声明
for ($x = 0; $x <=2000; $x++)
{

$degre[$x] = $degre[$x] + $step[$i]
}

将代码更改为

for ($i = 0; $i <=100; $i++)

{
my @xx = map {$aa[$_]*(cos($Degre[$_])*(pi/180))}(1..2000);
my @yy = map {$bb[$_]*(cos($Degre[$_])*(pi/180))}(1..2000);

#@Degre = map {$Degre[$_] + $step[$_]} (1..2000);
for ($x = 0; $x <=2000; $x++)

{
$Degre[$x] = $Degre[$x] + $step[$x];
}

给出以下输出

738.346205775827  646.171091419262  395.07480695473 0.484472140779317
738.346205775827  646.171091419262  395.559279095509 0.484472140779317
738.346205775827  646.171091419262  396.043751236288 0.484472140779317
738.346205775827  646.171091419262  396.528223377068 0.484472140779317
738.346205775827  646.171091419262  397.012695517847 0.484472140779317
738.346205775827  646.171091419262  397.497167658626 0.484472140779317
738.346205775827  646.171091419262  397.981639799406 0.484472140779317
738.346205775827  646.171091419262  398.466111940185 0.484472140779317

正如您所看到的那样,每次考虑循环时,度数列现在可以通过步长值正确递增。那么为什么地图不会这样做呢。

3 个答案:

答案 0 :(得分:3)

每次@Degre aabb保持不变时,您的step内部都会发生变化。

  print "$aa[1]  $bb[1]  $Degre[1] $step[1] \n";

以上显示

中计算的正确值
 @Degre = map {$Degre[$_] + $step[$_]} (1..2000);

在您的打印语句中打印@xx@yy,而不是@aa@bb,并根据计算查看值的变化。

@Degre=sort(@Degre);循环之前放置for并查看结果。以下是排序后的结果 -

1726.50146484375  681.5185546875  0.624771118164063 0.456436157226563 
1726.50146484375  681.5185546875  1.15629577636719 0.456436157226563 
1726.50146484375  681.5185546875  1.7493896484375 0.456436157226563 
1726.50146484375  681.5185546875  2.49296569824219 0.456436157226563 
1726.50146484375  681.5185546875  2.861083984375 0.456436157226563 
1726.50146484375  681.5185546875  3.20767211914063 0.456436157226563 
1726.50146484375  681.5185546875  3.44265747070313 0.456436157226563 
1726.50146484375  681.5185546875  11.8232574462891 0.456436157226563 
1726.50146484375  681.5185546875  12.2711944580078 0.456436157226563 
1726.50146484375  681.5185546875  12.3104858398438 0.456436157226563 
1726.50146484375  681.5185546875  13.2642059326172 0.456436157226563 
1726.50146484375  681.5185546875  13.4784698486328 0.456436157226563 
1726.50146484375  681.5185546875  103.224014282227 0.456436157226563 
1726.50146484375  681.5185546875  103.868133544922 0.456436157226563 
1726.50146484375  681.5185546875  104.103759765625 0.456436157226563 
1726.50146484375  681.5185546875  104.71240234375 0.456436157226563 

正如您所看到的,计算是正确的,即第一行0.624771118164063 + 0.456436157226563中的1.15629577636719数据是第二行中的3.44265747070313数据,依此类推。

但是,某些时候从11.8232574462891跳到@Degre = map {$Degre[$_] + $step[$_]} (0..@step-1); 。我不确定为什么会发生这种情况,但我的假设是索引在某个时刻出错了。使用以下代码解决此问题

1139.18230471501  192.716943394942  183.715442551381 0.45937396317494 
1139.18230471501  192.716943394942  184.174816514556 0.45937396317494 
1139.18230471501  192.716943394942  184.634190477731 0.45937396317494 
1139.18230471501  192.716943394942  185.093564440906 0.45937396317494 
1139.18230471501  192.716943394942  185.552938404081 0.45937396317494 
1139.18230471501  192.716943394942  186.012312367256 0.45937396317494 
1139.18230471501  192.716943394942  186.471686330431 0.45937396317494 
1139.18230471501  192.716943394942  186.931060293605 0.45937396317494 
1139.18230471501  192.716943394942  187.39043425678 0.45937396317494 
1139.18230471501  192.716943394942  187.849808219955 0.45937396317494 
1139.18230471501  192.716943394942  188.30918218313 0.45937396317494 

这次输出在第三列中更加一致 -

{{1}}

答案 1 :(得分:3)

添加

use strict;
use warnings;

我的输出结果非常不同:

Scalar value @Degre[$_] better written as $Degre[$_] at test3 line 20.
Scalar value @step[$_] better written as $step[$_] at test3 line 20.
Scalar value @bb[1] better written as $bb[1] at test3 line 23.
Scalar value @aa[1] better written as $aa[1] at test3 line 23.
Scalar value @Degre[1] better written as $Degre[1] at test3 line 23.
Scalar value @step[1] better written as $step[1] at test3 line 23.
Global symbol "$i" requires explicit package name at test3 line 14.
Global symbol "$i" requires explicit package name at test3 line 14.
Global symbol "$i" requires explicit package name at test3 line 14.
Execution of test3 aborted due to compilation errors.

答案 2 :(得分:1)

我认为你有@在哪里你的意思是$。

@Degre = map {@Degre[$_] + @step[$_]} (1..2000);

如果您正在尝试访问@Degre和@step的元素,那么您需要 $ Degre [$ _]等。

我不清楚@ xx / yy是什么 - 它们似乎没有被使用