filename substitution question

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Sat Mar 25 06:13:01 AEST 1989


Peter Johnson writes:
: I use a program written by Chris tweed called "sets".  This program
: just does set operations on its arguments.  So for example if you
: wanted to remove all files except those that end in .c or .h you
: can do something like this:
: 
:   rm `sets * -d *.c`
: 
: The -d stands for set difference.  Sets also supports intersection and union.
: 
: If you are interested, let me know and I will mail you a copy.

Just for the fun of it, here's about 1/2 hour's work in perl, one screenful.
I don't know if the tokenization or precedence is the same as the other
version, but this version of sets will do parens (the other one might too,
for all I know).  It's likely shorter.  And it would be trivial to add new
operators--one line apiece.

#!/usr/bin/perl
$setnum = 0;
$setstring = "\$set${setnum}_{\$_}";
while ($#ARGV >= 0) {
    $_ = shift;
    if (/^-[diu]$/) {
	++$setnum;
	$setstring = "\$set${setnum}_{\$_}";
	$expr .= '&&!'	if /^-d/;
	$expr .= '&&'	if /^-i/;
	$expr .= '||'	if /^-u/;
    }
    elsif (/^[()]$/) {
	$expr .= $_;
    }
    else {
	$universe{$_} = 1;
	$expr .= $setstring;  $setstring = '';
	eval "\$set${setnum}_{\$_} = 1;";
    }
}
eval "for (keys(universe)) { delete \$universe{\$_} unless $expr; }";
print join(' ',sort keys(universe)),"\n";

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.wizards mailing list