Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Nested loop with 2 counters for 2 array variables
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 06-18-2009, 02:16 PM #1 briana.paige LQ Newbie   Registered: May 2009 Location: Canada Posts: 14 Rep: Nested loop with 2 counters for 2 array variables Hi, I'm trying to get my perl script to write out an input file for building simulation software. It calculates the vertices of a 1x1x0.01m block in space based on the azimuth and tilt angle and writes it to a file. I've provided a shortened version of my code. I have two arrays: 1: azimuth angle of a PV panel 2: tilt angle of a PV panel Code: ```@azimuth = (-180, -165, -150, -135, -120, -105, -90, -75, -60, -45, -30, -15, 0); @tilt = (0, 15, 30, 40, 45, 50, 55, 60, 75, 90);``` my code runs if I use an array with only one element, for example: Code: ```\$azimuth[\$i] = -135; \$tilt[\$f] = 45;``` I would like it to enter the first loop (so \$azimuth[0] = 180), then run the second loop multiple times (\$tilt[\$f] = 0, \$tilt[\$f] = 15, \$tilt[\$f] = 30, etc...). Then I want it to move to \$azimuth[1] and do the same thing. Code: ```#! /usr/bin/perl -w #use strict; use Math::Trig;# ':pi'; use File::Copy; # set incrementers i and f for azimuth and tilt arrays respectively \$i = 0; \$f = 0; \$pi = atan2(1,1) * 4;; \$pip2 = \$pi/2; #define azimuthal angles (phi) in degrees \$azimuthd[\$i] = -135; \$azimuth[\$i] = deg2rad(\$azimuthd[\$i]); # define tilt angle (alpha) in degrees \$tiltd[\$f] = 45; \$tilt[\$f] = deg2rad(\$tiltd[\$f]); while(\$i < 1){ # azimuth loop - all tilt angles for each azimuth if(\$f < 1){ # tilt loop if ((-\$pip2 > \$azimuth[\$i]) && (\$azimuth[\$i] > -\$pi)) { \$bprojc = \$bc * cos(\$tilt[\$f]); \$projcx = \$bprojc * (cos(\$pi + \$azimuth[\$i])); \$projcy = \$bprojc * (sin(\$pi + \$azimuth[\$i])); \$cz = \$bc * ((sin(\$tilt[\$f]))); \$c[0] = \$bx - (\$projcx); \$c[1] = \$by + (\$projcy); \$c[2] = \$cz; } open(GEO, '>../zones/hal.geo'); # overwrite simulation input file printf GEO "# geometry of hal defined in: ../zones/hal.geo\n"; printf GEO "GEN hal hal describes a 1m2 pv panel # type, name, descr\n"; printf GEO " 8 6 ".sprintf("%.3f", \$azimuth[\$i])." # vertices, surfaces, rotation angle\n"; printf GEO "# X co-ord, Y co-ord, Z co-ord\n"; printf GEO " ".sprintf("%.5f", \$c[0]).""; printf GEO " ".sprintf("%.5f", \$c[1]).""; printf GEO " ".sprintf("%.5f", \$c[2])." # vert 2\n"; close (GEO); # close simulation input file system('bps.pl'); # run simulation copy "OUT.csv", "Ottawa_\$azimuthd[\$i]_\$tiltd[\$f].csv"; # copy results file \$f++; # increment tilt index n w/out changing azimuth angle } # end of tilt if loop \$i++; # increment the azimuth angle once all the tilt angles have been run } # end of for loop for the azimuth angle i.e., every tilt angle has been run for each azimuth angle.``` I've been experimenting with while, if, for, foreach loops with no luck. I'm trying to convert from degree (@azimuthd) inputs to radians (@azimuth) for the calculations and I don't believe that I'm doing that correctly either. @azimuthd was being defined correctly but @azimuth only contains the first element of @azimuthd. I tried the following but it seemed to only read in the first element: Code: `@azimuth = deg2rad(@azimuthd);` I know that once its run the tilt loop \$f should be reset for the next azimuth angle otherwise it won't pass the conditions but I'm really not sure how to go about it. Any input will be greatly appreciated, I hope I clearly laid out my problem.
 06-18-2009, 06:12 PM #2 chrism01 LQ Guru   Registered: Aug 2004 Location: Sydney Distribution: Centos 6.9, Centos 7.3 Posts: 17,374 Rep: Use a std for loop Code: ```@azimuth = (-180, -165, -150, -135, -120, -105, -90, -75, -60, -45, -30, -15, 0); @tilt = (0, 15, 30, 40, 45, 50, 55, 60, 75, 90); for \$a_angle ( @azimuth ) { for \$t_angle ( @tilt ) { # do calcn } }``` Code: ```# degrees to radians & reverse use Math::Trig; \$radians = deg2rad(\$degrees); \$degrees = rad2deg(\$radians);```
 06-19-2009, 07:38 AM #3 briana.paige LQ Newbie   Registered: May 2009 Location: Canada Posts: 14 Original Poster Rep: Thank-you very much! You've saved me a lot of time and headaches. Cheers!

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post xskycamefalling Programming 4 05-11-2009 03:14 PM novaraz Programming 6 10-25-2008 08:40 AM muazfarooqaslam Linux - Newbie 8 02-01-2008 08:03 AM Grafbak Programming 9 12-19-2006 10:28 AM Harry Seldon Programming 3 05-06-2006 05:15 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:36 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -