[clue-tech] [spam?] text processing howto

chris fedde chris at fedde.us
Thu Oct 7 00:32:11 MDT 2010


On Wed, Oct 6, 2010 at 8:38 PM, Collins Richey <crichey at gmail.com> wrote:
> On Wed, Oct 6, 2010 at 6:36 PM, Collins Richey <crichey at gmail.com> wrote:
>
>>
>> You can do the same thing in two lines of perl.
>
> Well, I lied about 2 lines; I forgot about printing. Anyway, in the
> much maligned perl code ...
>
> #!/usr/bin/perl
> # string has imbedded tabs and varying number of spaces
> $string = "this          that1   that2   that3          theotherthing";
> @list = split(/\s+/, $string);
> foreach $item (@list) {
>        print "$item\n";
> }
> # and if you want just the otherthing
> print "$list[$#list]\n"
>

Golf is a popular game among some parts of the perl community.
Getting the task done in the fewest number of (key) strikes. :-)
These solutions take advantage of default behaviors of the split
function.

# the last item of the list
perl -le '$_= "this          that1   that2   that3
theotherthing"; print +(reverse split)[0]'

# all the items one per line
perl -le '$_= "this          that1   that2   that3
theotherthing"; print for split'


More information about the clue-tech mailing list