I need some help with the map function in Perl, it seems to croupte my arrays.
#!/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";
}
Now the out put of this gives
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
With the 3rd column being the degrees, I don’t see why it seems to jump around randomly when I expected it to increase in 0.117635….. steps?
Cheers
UPDATE
To confirm I am trying to get the map statment to do the following
for ($x = 0; $x <=2000; $x++)
{
$degre[$x] = $degre[$x] + $step[$i]
}
altering the code to
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];
}
gives the following out put
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
as you can see the degree column now incremented correctly by the step value each time thought the loop. So why does map not do the same.
Your
@Degregets changed inside the loop every time where asaabbandstepremains unchanged.The above shows correct value as calculated in
Print
@xxand@yyin your print statement instead of@aaand@bband see the values change as per calculation.Put a
@Degre=sort(@Degre);just before theforloop and see the results. Below are the results after sorting-As you can see, the calculation is correct i.e. in the first row
0.624771118164063 + 0.456436157226563is1.15629577636719data in the second row and so on.However, there is a jump from
3.44265747070313to11.8232574462891at some point. I am not sure why that may be happening happening, but my assumption is that the indexing goes wrong at a certain point. This is resolved using code belowThis time the output is much more consistent in the third column-