CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
undefined array in foreach

 



phubaba
New User

Feb 5, 2010, 6:32 AM

Post #1 of 5 (787 views)
undefined array in foreach Can't Post

Hello everyone, I'm having a very weird problem, and I'm moderately new to perl but not new to programming.

The problem is as follows. The first time through the while loop everything works fine all the sql, reporting, and loading of databases work. However on the next time through the while loop, @orderLevelList is undefined with no values in it. I do not touch @orderLevelList anywhere in the while loop except for reading from it. I currently add a hack before foreach(@orderLevelList) that is essentially checking if @orderLevelList is undefined and if so call split(",", "order1,order2,order3") again. It is important to note that all the other arrays and variables still exist.

This code is paraphrased to give structure to the problem.

I am looking for any possible ideas on why this may happen? I greatly appreciate it!


Code
 @orderLevels = split(",", "order1,order2,order3"); #usually orders and regions are input by user so would be variable 
@regions = split(",", "US,EU,ASIA");
my $start = 0;
my $end = 100;while($start < $end){
foreach(@regions){
my $currentRegion = $_;

foreach(@orderLevels){
my $currentLevel = $_;

#call another perl script using system() that does a lot of sql and creates a lot of files

#from those files bcp them into some databases using some sql
#run some reporting from an R script on the data using system()
} }
my $start+=30;
}



FishMonger
Veteran / Moderator

Feb 5, 2010, 6:50 AM

Post #2 of 5 (785 views)
Re: [phubaba] undefined array in foreach [In reply to] Can't Post

The code you posted creates an infinite loop that repeats the following output lines (assuming you add a print statement).

Quote
order1
order2
order3


Please post a short but complete script that demonstrates the problem you describe and please use proper code formatting and indentation.


(This post was edited by FishMonger on Feb 5, 2010, 7:08 AM)


phubaba
New User

Feb 5, 2010, 7:51 AM

Post #3 of 5 (781 views)
Re: [FishMonger] undefined array in foreach [In reply to] Can't Post

hello thank you for the reply.



the code is actually much larger then this and I can't feasibly copy it all to you. My problem is that the orderLevelList is not set anywhere except the first split and is only read from then on. I'm just wondering if there are any bugs that could cause this variable to become undefined by calling sql loading sql or calling R.

Thanks,

Rob


FishMonger
Veteran / Moderator

Feb 5, 2010, 8:11 AM

Post #4 of 5 (780 views)
Re: [phubaba] undefined array in foreach [In reply to] Can't Post

The code you posted doesn't have an @orderLevelList array, so I can't say if/where it might get undefined.

If you can't post a short example that demonstrates the problem, then the only other thing I can suggest is to add some debug print statements at each and every point where that array is being used.


FishMonger
Veteran / Moderator

Feb 5, 2010, 9:17 AM

Post #5 of 5 (777 views)
Re: [phubaba] undefined array in foreach [In reply to] Can't Post


Quote
I'm just wondering if there are any bugs that could cause this variable to become undefined by calling sql loading sql or calling R.


No bugs in perl that would cause the array to be undefined.

The bug is in your code/logic.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives