Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl Map Function not working as expected
    primarykey
    data
    text
    <p>I need some help with the map function in Perl, it seems to croupte my arrays.</p> <pre><code>#!/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 &lt;=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"; } </code></pre> <p>Now the out put of this gives </p> <pre><code>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 </code></pre> <p>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? </p> <p>Cheers</p> <p>UPDATE </p> <p>To confirm I am trying to get the map statment to do the following </p> <pre><code>for ($x = 0; $x &lt;=2000; $x++) { $degre[$x] = $degre[$x] + $step[$i] } </code></pre> <p>altering the code to </p> <pre><code>for ($i = 0; $i &lt;=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 &lt;=2000; $x++) { $Degre[$x] = $Degre[$x] + $step[$x]; } </code></pre> <p>gives the following out put </p> <pre><code>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 </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload