Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:16:35+00:00 2026-06-09T06:16:35+00:00

I need some help with the map function in Perl, it seems to croupte

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:16:36+00:00Added an answer on June 9, 2026 at 6:16 am

    Your @Degre gets changed inside the loop every time where as aa bb and step remains unchanged.

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

    The above shows correct value as calculated in

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

    Print @xx and @yy in your print statement instead of @aa and @bb and see the values change as per calculation.

    Put a @Degre=sort(@Degre); just before the for loop and see the results. Below are the results after sorting-

    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 
    

    As you can see, the calculation is correct i.e. in the first row 0.624771118164063 + 0.456436157226563 is 1.15629577636719 data in the second row and so on.

    However, there is a jump from 3.44265747070313 to 11.8232574462891 at 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 below

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

    This time the output is much more consistent in the third column-

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this function and need some help sorting out the logic flow. It
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help assigning a mouseover event to display some icons that start out
Need some help with a query.. I have three tables. Source id name 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.