Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Perl Best Practices - Loop Labels

by tobyink (Canon)
on Apr 16, 2020 at 06:51 UTC ( [id://11115607]=note: print w/replies, xml ) Need Help??


in reply to Perl Best Practices - Loop Labels

The $work questions generally centred around whether jumping out of nested loops, starting the next iteration of a loop early, and so on, was a good practice.

So say you need to search through a bunch of files to see if Joe Bloggs is mentioned in any of them. You don't care which files he's mentioned in, or how many times. You just want a boolean — is he mentioned at all?

my $mentioned = 0; FILE: for my $file ( @files ) { LINE: for my $line ( @lines ) { /Joe Bloggs/ and ++$mentioned and last FILE; } } return $mentioned;

The question of whether it's good practice to jump out of nested loops becomes "after I've found the answer to my question, should I keep searching through the rest of the files?"

Or another way of thinking about it: "after I found my lost car keys, should I keep looking for them?"

I'm sure there are good times to jump out of loops and bad times to jump out of loops, and there are many subtle nuances. But in the general case, if you know a loop has served its purpose, jump out of it.

(Oh, and another thing. You'll notice I labelled my inner loop too, even though I never used that label. I find labelling loops, especially nested loops can be a form of documentation.)

Update:, please, please don't do this though:

my $mentioned = 0; FILE: for my $file ( @files ) { check_file($file, \$mentioned); } return $mentioned; ...; sub check_file { my ($file, $mentioned) = @_; LINE: for my $line ( @lines ) { /Joe Bloggs/ and ++$$mentioned and last FILE; } }

Yes, Perl does let last to be in a subroutine called from the loop. Don't do that. It's really hard to grok. Only use next, last, and redo lexically within the loop block they affect.

Replies are listed 'Best First'.
Re^2: Perl Best Practices - Loop Labels
by kcott (Archbishop) on Apr 16, 2020 at 07:57 UTC

    G'day tobyink,

    Your usage seems to align with mine. For single loops, I don't generally supply a label. For nested loops, I'll generally give all loops a label; although, if I'm not using next, last, etc. I'll probably omit the labels, e.g. for processing all cells in a grid:

    for my $row (1 .. $row_count) { for my $col (1 .. $col_count) { # process cell at row $row and column $col } }

    Not really related to loops but years ago, when working with junior programmers who didn't fully understand lexically scoped pragmata, I'd use labels to document anonymous blocks; usually something overt, such as:

    SUBROUTINE_REDEFINITION_BLOCK: { no warnings 'redefine'; sub subname { ... } }

    This followed several instances where braces delimiting anonymous blocks had been removed because "they looked like superfluous code".

    — Ken

Re^2: Perl Best Practices - Loop Labels
by talexb (Chancellor) on Apr 16, 2020 at 13:20 UTC
      (Oh, and another thing. You'll notice I labelled my inner loop too, even though I never used that label. I find labelling loops, especially nested loops can be a form of documentation.)

    Interesting -- my preference would be to not do that, because then my brain would be searching for where the LINE: label is used. If a label's not used, I wouldn't put it in. That's a matter of personal taste, I guess -- but also, this is a simplified example.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115607]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found