Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: breaking out of outer loops Date: Tue, 8 Mar 2016 10:47:16 +1100 Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de E+SI4+dU8KKew/81LzRgJASAuTqoPP+VFF8mbsoCP2jg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '#if': 0.05; 'column': 0.07; 'perl,': 0.07; 'skip:/ 10': 0.07; 'cc:addr:python- list': 0.09; '(defined': 0.09; 'loop.': 0.09; "'break'": 0.16; '2016': 0.16; 'after,': 0.16; 'clause,': 0.16; 'discarded': 0.16; 'fluent': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'subject:breaking': 0.16; 'wrote:': 0.16; 'fixed.': 0.18; 'skip': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'file:': 0.22; 'parsing': 0.22; 'trying': 0.22; 'am,': 0.23; 'bit': 0.23; "python's": 0.23; 'split': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header': 0.24; 'script': 0.25; 'skip:" 20': 0.26; 'equivalent': 0.27; 'message- id:@mail.gmail.com': 0.27; 'perl': 0.29; "i'm": 0.30; 'print': 0.30; '"the': 0.32; 'right?': 0.33; 'tue,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'mine': 0.35; 'but': 0.36; 'too': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'flow': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'does': 0.39; "didn't": 0.39; 'your': 0.60; 'skip:u 10': 0.61; 'here.': 0.62; 'mar': 0.65; 'statement,': 0.66; 'here': 0.66; 'skip:$ 10': 0.67; "'for'": 0.84; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=RuQc4fvy9PcM5nLDHitShc4Ipe5KtutvXuMeQ3k/NoM=; b=W8SQUe/xVaA+dy3usJHFqQJzaofR0W0uAgOlVtE/ahNTIn5yPPg5t80/nFQYJ8ktdf TjOC2+k0JvqqPZCQzQbmrbG4WmVRR5QR+aCYB40uAt9a7E60BktnMMzh1hB3+NC1Bsth hWcXAYXS6mh/un1kHrI0Y6pSCi4VX2AKc/RNG8JS1VydO2GHzjV3doEUHcoIyrJJ5u8i B2y1NWSoUa+RE/hlLEfR3kRK/SsFx+ASxoW9zxosVIE3EFk2vDw2+jM2O6LuK0aBTxBG B6ggS2sIcKvFevu2a1TB1g9i5ZG0sS3wsehHtg2R2V38HUyRzcAZdjEAkC3jaAdULrOX YzbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=RuQc4fvy9PcM5nLDHitShc4Ipe5KtutvXuMeQ3k/NoM=; b=lRHcFM1MF/2FKJtHIvBrCwjrs2sZW16JxHNnf55+SFH/hD2Ih4rtKp9TTviCldT6kR E9rFJa5JtY4vgloRkrU3oEAHbx5WZ4AcBE1y2tW2rqqodNC8GjzzPbbw/pnN4tve3mFw 07antLrWzhL02eVtkK8RIbcuJlw6OgjBn8RzySjdds2/TXmXPavHQyjeATur9BVGM/JO Xpdc7cBxyET2HSqPOzysXXbbMUPCHSmn0RooviNkM9STpcvt+FPC3j9uhzfskBpk93Fw RPJtEALaMUP2nGJrkI07XQRlyTmt31AIYrSAwOsEJIWmDsKPdeF7E8PrQkmT/gQuZZEL ZjYw== X-Gm-Message-State: AD7BkJJ0jHfk3eM7vLBwCdC+2kt3pSZ2BeUyBE7YPLjk6H49mg7clMicDSkVevJBgwt/+V8zyZC5Lv6HncgGvA== X-Received: by 10.50.137.35 with SMTP id qf3mr15470467igb.92.1457394436433; Mon, 07 Mar 2016 15:47:16 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104275 On Tue, Mar 8, 2016 at 10:09 AM, Fillmore wrote: > For the record, here's a Perl script of mine I'm trying to port...there may > be 'malformed' lines in a TSV file I'm parsing that are better discarded > than fixed. > > my $ctr = 0; > OUTER: > while($line = ) { > > $ctr++; > if ($ctr < 5) {next;} > > my @allVals = split /\t/,$line; > > my $newline; > foreach my $i (0..$#allVals) { > > if ($i == 0) { > if ($allVals[0] =~ /[^[:print:]]/) {next OUTER;} > > $newline = $allVals[0]; > } > > if (defined $headers{$i}) { > > #if column not a number, skip line > if ($allVals[$i+1] !~ /^\d+$/) {next OUTER;} > > $newline .= "\t".$allVals[$i+1]; > } > } > print $newline."\n"; > > } I'm not too fluent in Perl, but my understanding here is that "next OUTER" is equivalent to Python's 'continue' statement, right? So your flow control is roughly this: for _ in range(5): skip header row for line in file: for cell in split(line): if bad_cell_value: break # Skip out of the inner loop now else: print_stuff # We didn't break Does that look right? The key here is that a 'for' loop has an 'else' clause, which happens if there's no 'break' in the main loop. I think that's what you're after, here; you get to break out of "the loop and a little bit more", so to speak. The outer loop is actually immaterial here. ChrisA