Wednesday, July 21, 2004

For what it's worth, here's a perl script I whipped together to sort the words in a line. It will take either a list of words from the command line or will read from stdin and sort each individual line. Output is to stdout in both cases. I know it could be tightened up, but I'm not much of a perl programmer. Then again, perl isn't much of a programming language. If none of that meant anything to you, you don't care about this.

#!/usr/bin/perl -w

use Text::ParseWords;

# Sort Words In Line

if (@ARGV) # sort words given on the command line
{
    print sortLine(join(" ", @ARGV)),"\n";
}
else # sort from STDIN
{
    while (<>)
    {
        chop; # lose the newline
        print sortLine($_),"\n";
    }
}

sub sortLine
{   # fully-qualified subroutine name because it is a reserved word
    return join(" ", sort(Text::ParseWords::quotewords(shift)));
}

( programming )