[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Everything should be kept as simple as possible...




On Thu, 17 Jun 1999, Vicki Brown wrote:

> My entry into the discussion:
> 
>   #!perl
> 
>   open(IN, "names.txt") or die "Oops";
> 
>   @names = <IN>;
> 
>   # note this is an ASCII (case-sensitive) sort; you may want to
>   # "improve" on that
> 
>   @names = sort @names;
> 
>   foreach (@names) {
>     print;
> 	# note you could now split or substitute ", " for the || or...
>   }

What is wrong with this?

  cat names.txt|sed 's/||/, /g'|sort +0 +1

If you really, really wanted to use perl for this, you could do:

#!/usr/bin/perl
open(IN, "names.txt") or die "Ack";
print sort <IN>;

> And Merlin out-Aha'd me!
> 
> At 13:06 -0700 6/17/99, Randal L. Schwartz wrote:
> > @ARGV = qw(names.txt);
> > print sort <>; # would have worked as well

Okay, that's pretty good. Didn't know about the ARGV thing. But still,
there's someting to be said for a nice, simple pipe. My single-line pipe
does a more correct sort than the perl equivalents you posted because it
sorts last name before first, rather than sorting the line as a whole.