Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic Problem 2

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
A while ago I posted a question about a loop in a loop. Here is the basic structure:

Code:
foreach (@problem) {
  my ($company, $logical_name, $numberprgn, $assignment, $uis_elapsed_start_time, $close_time, $res_anal_code,$user_priority, $severity_code ) = split (/,/);
  $problem{$logical_name}=($_);
          foreach (@device) { 
                ($log_name, $type, $subtype, $uis_support_level, $uis_tier, $uis_fstatus, $uis_managed_date, $uis_deactivated_date ) = split (/,/);
                 if (exists $problem{$log_name}) {#Look for a match.
                      my @afh = ("$logical_name,$numberprgn,$assignment,$open_month,$close_month,$open_year,$close_year,$rpt_yr,$rpt_month,$res_anal_code,$severity_code,$type,$uis_support_level,$uis_tier,$uis_fstatus,$uis_managed_date,$uis_deactivated_date");
                      $merge{$numberprgn} = [@afh]; 
                       last;
                  }else{
                        #Set defaults since a match was not found.
                         my @afh = ("$logical_name,$numberprgn,$assignment,$open_month,$close_month,$open_year,$close_year,$rpt_yr,$rpt_month,$res_anal_code,$severity_code,$type,$uis_support_level,$uis_tier,$uis_fstatus,$uis_managed_date,$uis_deactivated_date");
                         $merge{$numberprgn} = [@afh]; 
                         last;    
}

The "last" is the problem. For each enrty in @problem I need to search through each enrty in @device and look for a match for $log_name eq $logical_name and if found, populate the %merge hash with the real values, if not found, assign defaults to all variables.

The way my loop is working, it only gets to the first entry in @device as the "last" command is always encountered.

Thanks,

Nick

I got a Biz Degree! How the h*ll did I get here?
 
You might like to re-check your code. The statements in the if block and the else block are identical.
 
You need to remove the "last;" from both if an the else block in order for the foreach to loop through each device. Since you have last in both blocks, it will only process the first device.


Michael Libeson
 
Thanks for the replies.

Tony, The difference is in the:

#Set defaults since a match was not found.

This represents the code that sets default values for each variable from the device loop split.

Mlibeson,

If I remove the "last;" from both loops, a record is generated for each entry in the device table. Hence my problem, I only want to generate 1 record, either find a match from the @device $log_name with the $logical_name from the @problem loop and assign the real value from the @device list or "else" assign defaults.

Thanks,

Nick

I got a Biz Degree! How the h*ll did I get here?
 
OK, I'll rephrase that. Both code blocks are identical except for the comments.

You're also missing two closing curly brackets.
 
Tony,

That is not all my code. There are alot of "next unless" and "if" staments in each loop, but they do not apply to my logic problem and I did not want to complicate the code needlessly. You are right about the closing braces. I just cut and pasted the applicable code. The braces are in my real code.

Sorry for the confusion.

BTW..this is the last big hurdle I have to get over to complete this 4 month project and then we can ALL forget about ticket charts!

[thumbsup2]

I got a Biz Degree! How the h*ll did I get here?
 
In the case leave the last for the if block and remove the default else block from the foreach loop. Use a variable before the foreach loop for devices and set it to 0. change the variable to 1 if you enter the if block. After the foreach loop check the variable to see if it is still 0. If it is 0 then apply default block of code.


Michael Libeson
 
Thanks Michael, thats what I did.

Nick

I got a Biz Degree! How the h*ll did I get here?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top