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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exiting out of a loop?

Status
Not open for further replies.

sdslrn123

Technical User
Jan 17, 2006
78
GB
Getting a bit stuck. Would appreciate any help?
Thanks


I have a list of names.
Each name corresponds to a profile on a txt document.
Each txt document profile contains a section called address
I am only interested in those names that are in Texas as well as those specifically in Austin, Texas.

File format of Address section in profile
N.B The city and state are always abbreviated to 8 letters

Address: 34
Address: CITY
Address: STATE


I can group all the names so that I know how many are in Texas. But I am trying to further separate this into two groups one where AUSTIN is the city.

Any ideas?


Code:
foreach $name (@WEBSITE) {
foreach $line (@ADDRESS){
	    if ($line =~ /^Address/){
	    $address = substr($line,9,8);
            if ($address eq "TEXAS"){
open (ADDRESSBOOK, ">address.txt");
push (@ADDRESS, $name);
print ADDRESSBOOK "@ADDRESS\n";
$counter++;
last;
}
}
}
}
 
if you want to exit the first loop from within the second loop, use a label:

Code:
[b]MAINLOOP:[/b] foreach $name (@WEBSITE) {
   foreach $line (@ADDRESS){
      if ($line =~ /^Address/){
         $address = substr($line,9,8);
         if ($address eq "TEXAS"){
            open (ADDRESSBOOK, ">address.txt");
            push (@ADDRESS, $name);
            print ADDRESSBOOK "@ADDRESS\n";
            $counter++;
            last [b]MAINLOOP[/b];
         }
      }
   }
}

- Kevin, perl coder unexceptional!
 
For the perler who wants to try, here is a formatted version his code.

I see problems with it already. @ADDRESS is not dependent on @WEBSITE. he modifies @ADDRESS while in the foreach loop. I haven't even bothered trying to understand what he's trying to do yet. Maybe want of y'all want to give it a go:

Code:
foreach $name (@WEBSITE) {
	foreach $line (@ADDRESS) {
        if ($line =~ /^Address/) {
	        $address = substr($line,9,8);
            if ($address eq "TEXAS") {
				open (ADDRESSBOOK, ">address.txt");
				push (@ADDRESS, $name);
				print ADDRESSBOOK "@ADDRESS\n";
				$counter++;
				last;
			}
		}
	}
}
 
I haven't even bothered trying to understand what he's trying to do yet.

hehehe....


if (/!understand/ {
[flowerface] && last;
}
else {
[banghead] && next;
}

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top