0

我需要一些关于 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

第三列是度数,我不明白为什么当我预计它会以 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

正如您所看到的,度数列现在每次考虑循环时都会按步长值正确递增。那么为什么地图不做同样的事情。

4

3 回答 3

3

@Degre每次 asaa bbstep保持不变时,您都会在循环内更改。

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

以上显示了正确的值,如

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

在您的打印语句中打印@xxand而不是and并查看值根据计算发生变化。@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 
于 2012-08-08T22:16:56.227 回答
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.
于 2012-08-08T22:18:55.817 回答
1

我想你有@,你的意思是$。

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

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

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

于 2012-08-08T22:15:57.290 回答