Re: Small golfing trick
[email protected] (Ton Hospel) Sun, 2 Apr 2006 10:20:33 +0000 (UTC)
| Newsgroups | gmane.comp.lang.perl.golf |
|---|---|
| Organization | lunix confusion services |
| Message-ID | <[email protected]> |
In article <[email protected]>, Eirik Berg Hanssen <[email protected]> writes: > [email protected] (Philippe 'BooK' Bruhat) writes: > >> Compare the classic "remove duplicate lines" one-liner >> >> perl -ne'$s{$_}++||print' file >> >> with >> >> perl -ne'$$_++||print' file >> >> Three characters in one shot! > > It has its limitations though. Like namespaces: > > ~$ perl -e 'print"a\n::a\nmain::a\n::main::a\n"; ' | perl -ne'$$_++||print' > a > ~$ > > ... and missing newlines: > > ~$ perl -e 'print"a\n1"; ' | perl -ne'$$_++||print' > a > Modification of a read-only value attempted at -e line 1, <> line 2. > ~$ > > > Eirik In real golfing the limitation that bites you most is the direct version of what causes the problem above: $$_ doesn't work as storage space if $_ is a postive integer. Though this can sometimes be worked around by using @$_ or $#$_