Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Unique strings

by tye (Sage)
on Aug 20, 2002 at 18:57 UTC ( [id://191562]=CUFP: print w/replies, xml ) Need Help??

Here is a rather golfed way to get just the unique strings from an array. It also saves the hassle of naming a temporary hash variable. Of course, it doesn't preserve order and it converts non-string items such as objects into strings while only running half as fast. (:
keys%{{@list,reverse@list}}

Replies are listed 'Best First'.
Re: Unique strings
by sauoq (Abbot) on Aug 21, 2002 at 02:23 UTC

    Three under:

    keys%{{map{$_,$_}@list}}

    Update:

    keys%{{@A,reverse@A}} # Then again... keys%{{map{$_,$_}@A}} # Maybe it's a tie with tye.

    Update 2:

    keys%{{map{$_,1}@A}} # Oh duh! OK. One under. :-)
    -sauoq
    "My two cents aren't worth a dime.";
    

      Actually, that was about what I started with. I wasn't really 'golfing', more like 'goofing' -- wanting something short (sure), but also unusual/idiomatic, and either really fast or really slow. It went something like this:     keys%{map{$_=>1}@list} "Way too conventional. Almost clear. And I don't like the 'map{}'."     keys%{map $_,0, @list} "Better, except it doesn't work. That comma is a problem. How to get rid of it... Oh!"     keys%{map$_=>0,@list} "Hrm, that's almost back were I started. D'oh! And it doesn't work, either. Ooh, I'll use 'x' instead!"     keys%{map$_ x2,@list} "Oops, I mean..."     keys%{map ($_)x2,@list} "Uhh... no. I hate that rule! I liked it better back when we didn't try to pretend that whitespace didn't sometimes have syntactic significance."     keys%{map+($_)x2,@list} "Not bad. But I don't like 'func+(' much. Heck, I don't like 1-to-many map, anyway (it still feels 'slow' to me and yet isn't slow enough). Maybe I'll go for maximum speed, maximum functionality (preserve order and references)..."     do{my%h;grep!$h{$_}++,@list} (Actually, I didn't get it nearly that tight before giving up) "Hrm, don't like that '@list' in the middle. Maybe..."     sub{my%h;grep!$h{$_}++,@_}->(@list) "Er, that didn't help. Okay, forget 'maximum functionality'!"     keys%{@list[...]} "But what to put for '...'? Too bad things like @list[0..-1] don't work..."     keys%{@x[0..$#x,1..$#x]} "Hrm... And doesn't work for odd sizes..."     keys%{@x,reverse@x} "'reverse' is such a long name for a function that can be so useful in golf. Oh, I'm not golfing. Yeah, that's not bad. It works for even and odds sizes but kinda 'for different reasons'. It might even be faster than a 1-to-many map version... maybe. Heck, let's see..."

      ...just type in a benchmark script directly into 'perl' since I have console line editing...

      "Wow! It's 75% faster than even using the usually fastest @hash{@list}= () trick! Maybe it is worth posting."

      ...compose and preview post...

      "Oh, if I'm going to publicly claim 'fastest', I should verify that the two solutions produce the same results... Huh? Why is that the empty list? Oh, '%{}' isn't enough, I need '%{{}}'!"

      ...rerun benchmark...

      "D'oh! 50% slower... Ah, heck, it is still kinda goofy. I'll post it anyway."

      And, oh, congratulations and ++sauoq! Nice golfing! (:

              - tye (but my friends call me "Tye")
Re: Unique strings
by rattusillegitimus (Friar) on Aug 20, 2002 at 19:20 UTC

    Doesn't keys %{{@list}} only check every other item in the list (a hash being a list like key1, val1, key2, val2...)?

Re: Unique strings
by Aristotle (Chancellor) on Aug 20, 2002 at 19:12 UTC
    Am I colossally blind? I would think keys %{{@list}} suffices. I can't see any reason for the reverse @list part.. Update: oow, d'uh. dpuu++

    Makeshifts last the longest.

      A hash needs both keys and values. the reverse ensures each element appears as a key, somewhere. Otherwise you lose half the elements. --Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://191562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found