jch341277 has asked for the wisdom of the Perl Monks concerning the following question:
I've been thinking for a while that there should be a fairly simple way for doing the same thing as the shell idiom:
$ cat file.txt|sort|uniq
Here's what I've come up with to sort a list of phone numbers:
my $l = 0; map { defined $_ and print "$_\n" } map { ($_->[1] ne $l)? $l=$_->[1] : undef } sort { $a->[1] <=> $b->[1] } map { [s/[\D\n]//g, $_] } <>;
But you could use it to sort strings just as easily. You probably don't want to use this to sort any really big files.
I'd be interested in knowing if anyone has found other ways to do this...
Update:After reviewing all the other ways to do this I have to let on that I've been playing with Schwartzian transforms lately so that's why mine took this overly complicated form.
The one-liners are great - however, even after doing a super search for %_ I still can't figure out why doing: @_{@telephones}=(); initializes %_ from the array @telephones?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: a way to do 'sort|uniq'
by rnahi (Curate) on Aug 02, 2005 at 15:35 UTC | |
by xdg (Monsignor) on Aug 02, 2005 at 18:38 UTC | |
|
Re: a way to do 'sort|uniq'
by merlyn (Sage) on Aug 02, 2005 at 15:40 UTC | |
by ikegami (Patriarch) on Aug 02, 2005 at 15:48 UTC | |
by tlm (Prior) on Aug 03, 2005 at 03:48 UTC | |
by merlyn (Sage) on Aug 03, 2005 at 06:47 UTC | |
by tlm (Prior) on Aug 03, 2005 at 11:12 UTC | |
by jch341277 (Sexton) on Aug 02, 2005 at 16:30 UTC | |
|
Re: a way to do 'sort|uniq' (efficient)
by tye (Sage) on Aug 02, 2005 at 18:26 UTC | |
|
Re: a way to do 'sort|uniq'
by sh1tn (Priest) on Aug 02, 2005 at 16:43 UTC | |
by jch341277 (Sexton) on Aug 02, 2005 at 18:35 UTC | |
by sh1tn (Priest) on Aug 02, 2005 at 19:12 UTC | |
|
Re: a way to do 'sort|uniq'
by Roy Johnson (Monsignor) on Aug 02, 2005 at 17:56 UTC | |
|
Re: a way to do 'sort|uniq'
by Anonymous Monk on Aug 02, 2005 at 16:45 UTC |