LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-18-2009, 03:16 PM   #1
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Rep: Reputation: 0
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.
 
Old 06-18-2009, 07:12 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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);
 
Old 06-19-2009, 08:38 AM   #3
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Original Poster
Rep: Reputation: 0
Thank-you very much! You've saved me a lot of time and headaches.
Cheers!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting problem with nested if statements in while loop error xskycamefalling Programming 4 05-11-2009 04:14 PM
Why does this loop clear variables? novaraz Programming 6 10-25-2008 09:40 AM
Using for loop on 2 variables muazfarooqaslam Linux - Newbie 8 02-01-2008 09:03 AM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 11:28 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 06:15 PM


All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration