Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Small golfing trick

22 views
Skip to first unread message

Philippe 'BooK' Bruhat

unread,
Mar 23, 2006, 5:40:23 PM3/23/06
to go...@perl.org
Hi,

I just discovered this, and do not know if it's commonly used in golf
(it's been a long time since I last played): if you only need a single
hash in your script, you can use the symbol table itself.

Compare the classic "remove duplicate lines" one-liner

perl -ne'$s{$_}++||print' file

with

perl -ne'$$_++||print' file

Three characters in one shot!

--
Philippe "BooK" Bruhat

The learned man makes a mistake but once... but the truly stupid keep
practicing until they get it right.
(Moral from Groo The Wanderer #75 (Epic))

Jasper

unread,
Mar 24, 2006, 4:14:18 AM3/24/06
to Philippe 'BooK' Bruhat, go...@perl.org
On 3/23/06, Philippe 'BooK' Bruhat <philipp...@free.fr> wrote:

> perl -ne'$s{$_}++||print' file
>
> with
>
> perl -ne'$$_++||print' file
>
> Three characters in one shot!

I think MTV used this in the palindrome golf, at least. So it's been
around for a while :)

Jasper

Philippe 'BooK' Bruhat

unread,
Mar 24, 2006, 5:24:33 AM3/24/06
to go...@perl.org
Le vendredi 24 mars 2006 à 09:14, Jasper écrivait:

I was pretty sure this wasn't original, but I liked the feeling when I
thought "by the way, the symbol table is just a hash!".

With the help of :: (or rather '), one can even build up nested data
structures, I think...

--
Philippe "BooK" Bruhat

Your reputation is what you make of it... and what you choose to take with
you. (Moral from Groo The Wanderer #48 (Epic))

j...@natura.di.uminho.pt

unread,
Mar 31, 2006, 5:15:08 AM3/31/06
to Philippe 'BooK' Bruhat, go...@perl.org
* Philippe 'BooK' Bruhat (philipp...@free.fr) wrote:
> Hi,
>
> I just discovered this, and do not know if it's commonly used in golf
> (it's been a long time since I last played): if you only need a single
> hash in your script, you can use the symbol table itself.
>
> Compare the classic "remove duplicate lines" one-liner
>
> perl -ne'$s{$_}++||print' file
>
> with
>
> perl -ne'$$_++||print' file

Whoo, nice :-) I'll add it to my notes, thanks :-)

> Three characters in one shot!
>
> --
> Philippe "BooK" Bruhat
>
> The learned man makes a mistake but once... but the truly stupid keep
> practicing until they get it right.
> (Moral from Groo The Wanderer #75 (Epic))

> _______________________________________________
> Cog-book mailing list
> Cog-...@perl-hackers.net
> http://perl-hackers.net/mailman/listinfo/cog-book
--
Jose Alves de Castro <c...@cpan.org>
http://jose-castro.org/

Eirik Berg Hanssen

unread,
Mar 31, 2006, 9:09:12 AM3/31/06
to Philippe 'BooK' Bruhat, go...@perl.org
philipp...@free.fr (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
--
For every complex problem, there is a solution that is simple, neat, and wrong.
-- H. L. Mencken
A good plan today is better than a perfect plan tomorrow.
-- Patton

Ton Hospel

unread,
Apr 2, 2006, 6:20:33 AM4/2/06
to go...@perl.org
In article <7xwteaw...@blackbox.eirik.dav>,

Eirik Berg Hanssen <Eirik-Ber...@allverden.no> writes:
> philipp...@free.fr (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 $#$_
0 new messages