[geeks] script language advice

Phil Stracchino alaric at metrocast.net
Sat Feb 2 16:11:42 CST 2008


Jonathan C. Patschke wrote:
> On Sat, 2 Feb 2008, Nadine Miller wrote:
> 
>> num of dupes * filesize  /path/to/file/filename /path/to/file/filename2
>> /path/to/filename3 [...]
> 
> Ah, the perl code you'd want is something like:
> 
>    while (<>) {                              # Snag a line from stdin
>          chomp;                              # Trim the line-ending
>          my @components = split(/\s/);       # Split into a list at spaces
>          my $dupcount   = shift @components; # Remove the first field.
>          my $trash1     = shift @components; # Remove the second field.
>          my $trash2     = shift @components; # Remove the third field.
>          my $fileToKeep = shift @components; # Remove the fourth field.
> 
>          unlink @components;                 # Delete everything else.
>    }

Rather inefficient.

my $l;
while (<>) {
     chomp;
     $l = split(/\s+/);
     unlink(splice($l,4));
}

It could be done in a single line without the temporary variable, but 
you'd have to force the list output by split() into the array type that 
splice() expects as its first argument, and I don't offhand recall the 
trick.  Only Larry Wall knows why the conversion is done automagically 
if the operation is broken into two statements, but not if it's 
condensed into one.


-- 
   Phil Stracchino, CDK#2         ICBM: 43.5607, -71.355
   Renaissance Man, Unix ronin, Perl hacker, Free Stater
   alaric at caerllewys.net            alaric at metrocast.net
           It's not the years, it's the mileage.



More information about the geeks mailing list