Re: removing duplicates



From: "John W. Krahn" <krahnj@xxxxxxxxx>
Jenda Krynicky wrote:
From: "John W. Krahn" <krahnj@xxxxxxxxx>
Rob Dixon wrote:
obdulio santana wrote:
May be this helps

perl -lne "print if ++$D{$_} == 1" address.txt
You may prefer the cuteness of

perl -lne "print unless $D{$_}++" address.txt
Or the shortness of:

perl -ne'$D{$_}++||print' address.txt

Actually this

perl -n'${$_}||=print'

works as well as long as the last line in the file either does end
with a newline or doesn't match any builtin variable whose default
value is already true.

One more keystroke down.

$ perl -c -n'$D{$_}++||print'
Unrecognized switch: -$D{$_}++||print (-h will show valid options).

Sorry, it should have been

perl -ne'${$_}||=print'

I'm using windows so I have to test it with doublequotes in place of
the singlequotes so I must have made a mistake changing them back.


From: "Chas. Owens" <chas.owens@xxxxxxxxx>
That is nasty. If I am reading that correctly you are using a
symbolic reference to create variables with the name of the line. > I can't remember, does Perl have a limit on the size of a variable > name?

Yes, you are reading it correctly. And since the line contains the
newline it can't match any builtin. Except possibly for the last line
in the file.

Does it have a limit on the size of a hash key?

If it does, then the limit is high enough I believe.

Jenda


===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: +< mode for opening files
    ... // I am attempting to understand what is the use of opening files for both ... What version of Perl, and what OS? ... will read starting from the point where the file pointer is at - and up ... which is right after the newline. ...
    (comp.lang.perl.misc)
  • Re: Newbie question on split, and also awk.
    ... to get working after a certain amount of self-abuse, with the exception of ... I'm pulling data lists from the OS, and parsing them in perl. ... You could just include the newline in the character class: ...
    (comp.lang.perl.misc)
  • Re: Absolute Novice (Mac Tiger) Needs Help
    ... leaving perl nothing to execute. ... but I thought that \r does represent a newline on ... translation of newline chars so that the Perl programmer didn't have to ... described in the first paragraph, and not broken as described in the ...
    (comp.lang.perl.misc)
  • Re: replacing double carriage returns with a single one
    ... Note that sed will probably add a trailing newline for non-empty input ... For converting double carriage returns to a single carriage ... If the character to be squashed from double to single isn't newline, ... change \n in the above bit of perl to the appropriate character ...
    (comp.unix.shell)
  • Re: Absolute Novice (Mac Tiger) Needs Help
    ... leaving perl nothing to execute. ... but I thought that \r does represent a newline on ... translation of newline chars so that the Perl programmer didn't have to ... operating system that the Perl program was running on. ...
    (comp.lang.perl.misc)