[m-dev.] for review: make w3 search pages consistent

Tyson Dowd trd at cs.mu.OZ.AU
Tue Dec 7 01:30:22 AEDT 1999


> Index: bin/divide
> ===================================================================
> RCS file: divide
> diff -N divide
> --- /dev/null	Wed May 28 10:49:58 1997
> +++ divide	Mon Dec  6 18:15:08 1999
> @@ -0,0 +1,28 @@
> +#!/usr/bin/perl
> +#
> +# divide <string> <filenames>
> +#
> +# Every time <string> is encountered on stdin, the next lines read in
> +# will be output to the next file in <filenames>
> +
> +$divide_string = $ARGV[0];
> +$i = 1;
> +
> +$switch = "no";
> +
> +open(OUTPUT, ">$ARGV[$i]") or die "Can't open file: $!";
> +
> +while (<STDIN>) {
> +	if ($_ =~ /$divide_string/) {
> +		$switch = "yes";
> +	}
> +
> +	if ($switch eq "no") {
> +		print OUTPUT $_
> +	}
> +    else {
> +        $switch = "no";
> +        $i = $i + 1;
> +	open(OUTPUT, ">$ARGV[$i]") or die "Can't open file: $!";
> +    }
> +}

Ah, the joys of perl.  Don't forget to use the default variable and the
default regex comparison operator, and we can move the else case
directly into the first test.


#!/usr/bin/perl

$divide_string = $ARGV[0];
$i = 1;

open(OUTPUT, ">$ARGV[$i]") or die "Can't open file: $!";

while (<STDIN>) {
        if (/$divide_string/) {
                open(OUTPUT, ">$ARGV[++$i]") or die "Can't open file: $!";
        } else {
                print OUTPUT
        }
}

Of course a real perl hacker would just do:

while (<STDIN>) {
	/$divide_string/ ? (open(OUTPUT, ">$ARGV[++$i]") or die "Can't open file: $!") : print OUTPUT;
}

(I'd be interested to know if there is a shorter way to do it).
Other than that, it looks fine.

Tyson.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list