Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

PERL STANDS FOR

Name: Anonymous 2006-10-10 20:53

(゚Д゚) POORLY EMULATOED RUBY

Name: Anonymous 2006-10-12 8:59 (sage)

From the Perl manual:

Here's how a C programmer might code up a particular algorithm in Perl:

    for (my $i = 0; $i < @ary1; $i++) {
    for (my $j = 0; $j < @ary2; $j++) {
        if ($ary1[$i] > $ary2[$j]) {
        last; # can't go to outer :-(
        }
        $ary1[$i] += $ary2[$j];
    }
    # this is where that last takes me
    }

Whereas here's how a Perl programmer more comfortable with the idiom might do it:

    OUTER: for my $wid (@ary1) {
    INNER:   for my $jet (@ary2) {
        next OUTER if $wid > $jet;
        $wid += $jet;
         }
      }

See how much easier this is? It's cleaner, safer, and faster. It's cleaner because it's less noisy. It's safer because if code gets added between the inner and outer loops later on, the new code won't be accidentally executed. The next explicitly iterates the other loop rather than merely terminating the inner one. And it's faster because Perl executes a foreach statement more rapidly than it would the equivalent for loop.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List