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!

Need to find One match then jump to outside loop 2

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Hello all,

I have two lists from DB queries with one common variable for comparison for a match. What I want to do is loop through each entry in the first list @problem and check for a match in the second list @device and if a match is not found, assign default values to the @device variables and then go to the next entry in @problem. Heres what I have

Code:
foreach $prob (@problem) {
    my ($common_prob, $probvar1, $probvar2, $probvar3) = split (/,/, $prob);
       foreach $dev (@device)
            my ($common_dev, $devar1, $devar2, $devar3) = split (/,/,$dev);
        if ($common_dev eq $common_prob) {
            $myhash{$probvar1} = ("$probvar2,$probvar3,$devar1,$devar2,devar3");
        }else{
             $devar1 = "default1";
             $devar2 = "default2";
             $devar3 = "default3";            
             $myhash{$probvar1} = ("$probvar2, $probvar3, $devar1, $devar2, $devar3");

I got a Biz Degree! How the h*ll did I get here?
 
A good example i found is this

foreach $user (@users) {
if ($user eq "root" or $user eq "lp") {
next;
}
if ($user eq "special") {
print "Found the special account.\n";
# do some processing
last;
}
}


basically use the "Last" command to force end of Foreach, however i've not tried this so i hope it works
 
Posted before I finished.

As you can see this will not work. I somehow need to break out of the "foreach $dev (@device) after the defaults have been assigned for the entry in the device table and go to the next problem table entry. The way I have it, Foreach entry in the problem table a record is generated for [bold] each enrty [/bold] in the device table. One way I see is the:

next LOOP;

after the default assignments for the @device loop, but I am not sure if that is the best way and if so, where do I put the loop?

Code:
LOOP: foreach $prob (@problem) {
    my ($common_prob, $probvar1, $probvar2, $probvar3) = split (/,/, $prob);
       foreach $dev (@device)
            my ($common_dev, $devar1, $devar2, $devar3) = split (/,/,$dev);
        if ($common_dev eq $common_prob) {
            $myhash{$probvar1} = ("$probvar2,$probvar3,$devar1,$devar2,devar3");
        }else{
             $devar1 = "default1";
             $devar2 = "default2";
             $devar3 = "default3";            
             $myhash{$probvar1} = ("$probvar2, $probvar3, $devar1, $devar2, $devar3");
              next LOOP;

I do not think that wil work either.

thanks

I got a Biz Degree! How the h*ll did I get here?
 
IDMF,

I do not think that will work.

Code:
foreach $user (@users) {
    if ($user eq "root" or $user eq "lp") {
        next;
    }
    if ($user eq "special") {
        print "Found the special account.\n";
        # do some processing
        last;
    }
}

The idea is on target but if I follow the logic:

foreach $user (@users)
if ($user eq "root" or $user eq "lp") {
next;
}

what that is saying is "if you find a match, go to the next $user and keep looking". That is not what I want.


I got a Biz Degree! How the h*ll did I get here?
 
Nick,

It's the second bit,
Code:
    if ($user eq "special") {
        print "Found the special account.\n";
        # do some processing
        last;[COLOR=red]#exit loop[/color]
    }

HTH
--Paul

cigless ...
 
Sorry I didn't mean to confuse you - the code wasn't the answer it was an example of the use of the command NEXT; and LAST;
 
Thanks Paul, but shouldn't it be something like this?

Code:
foreach $user (@users) {
    next unless ($user eq "root" or $user eq "lp") {
        # do some processing
    }
    else{
        # assign default values
        last;
    }
}

I got a Biz Degree! How the h*ll did I get here?
 
Thanks Paul and IDMF. Ths seems to work:

Code:
foreach $prob (@problem) {
    my ($common_prob, $probvar1, $probvar2, $probvar3) = split (/,/, $prob);
       foreach $dev (@device)
            my ($common_dev, $devar1, $devar2, $devar3) = split (/,/,$dev);
        if ($common_dev eq $common_prob) {
            $myhash{$probvar1} = ("$probvar2,$probvar3,$devar1,$devar2,devar3");
             last;
         }else{
             $devar1 = "default1";
             $devar2 = "default2";
             $devar3 = "default3";            
             $myhash{$probvar1} = ("$probvar2, $probvar3, $devar1, $devar2, $devar3");
             last;

If it finds a match...last, get out of loop, back to next problem....if it does not assign defaults and last, back to next poblem.

Nick

I got a Biz Degree! How the h*ll did I get here?
 
I'd be looking at hashes, because using an array in that manner leads to a lot of unnecessary processing
Code:
foreach $dev (@device) {[COLOR=red]#build up device hash[/color]
  my ($common_dev, $devar1, $devar2, $devar3) = split (/,/,$dev);
  $device{$common_dev}=$dev;
}
my $defaults="default1,default2,default3";
foreach $prob (@problem) {
  my ($common_prob, $probvar1, $probvar2, $probvar3) = split (/,/, $prob);
  if (exists $device{$common_prob}) {[COLOR=red]#look up device hash for common key to device and problem[/color]
    $myhash{$probvar1} = ("$probvar2,$probvar3,$devar1,$devar2,devar3");
  }else{
#    $devar1 = "default1"; [COLOR=red]#moved assignment of defaults outside loop to avoid redundancy[/color]
 #   $devar2 = "default2";
  #  $devar3 = "default3";            
    $myhash{$probvar1} = ("$probvar2, $probvar3, defaults");
  }
}

HTH
--Paul

cigless ...
 
Should read
Code:
$myhash{$probvar1} = ("$probvar2, $probvar3, [b][COLOR=red]$[/color][/b]defaults");

my bad
--Paul

cigless ...
 
and don't forget ...

$myhash{$probvar1} =
("$probvar2,$probvar3,$devar1,$devar2,devar3");

==========================/\

Missing $ there also :)
 
Thanks for the replies.

Nick

I got a Biz Degree! How the h*ll did I get here?
 
Thanks Paul. i modified my script to use a hash as suggested.

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