IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New grep is cool...
My favorite is still
$seen = ();\n@uniq = grep { !$seen{$_}++ } @stuff;


This is somewhat nifty in Python 2.4, if you don't care about the order of the unique list:
>>> stuff = (1,2,3,4,3,4,5,6,45,6,7,8,9,5,6)\n>>> print set(stuff)\n(1, 2, 3, 4, 5, 6, 7, 8, 9, 45)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Sure beats the old way
>>> stuff = (1,2,3,4,3,4,5,6,45,6,7,8,9,5,6)
>>> dict.fromkeys(stuff).keys()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 45]

Sets are available in 2.3, by the way, just as a library module instead of a builtin. But you knew that. But not everyone does. ;)
New my %seen; # Not $seen = ();
I assume that you want to be sure that %seen is an empty hash, not that the scalar $seen is set to be undef.

Using strict catches this common typo.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New *shrug* I got that from the Perl cookbook.
And it does work, incidentally.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Yes it does work. Once.
Hashes start off empty. But if you put your code into a subroutine and call it twice, the second time around it will still be populated from the first.

Incidentally I'm willing to bet, sight unseen, that the actual code in the cookbook got this right. :-P

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Nope, you're right.
I misread % as $; the cookbook has it correct.

One more reason not to use Perl: it sucks for people with bad eyes. ;-)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
     Perl array question - (pwhysall) - (20)
         if (grep {$_ eq $thing} @stuff) {...} - (ben_tilly) - (11)
             Thanks - (pwhysall)
             grep is cool... - (admin) - (5)
                 Sure beats the old way - (FuManChu)
                 my %seen; # Not $seen = (); - (ben_tilly) - (3)
                     *shrug* I got that from the Perl cookbook. - (admin) - (2)
                         Yes it does work. Once. - (ben_tilly) - (1)
                             Nope, you're right. - (admin)
             Why I Needed Something Other Than "grep" - (pwhysall) - (3)
                 You may only need one loop... - (ben_tilly) - (1)
                     And there's the rub. - (pwhysall)
                 Post the code - (broomberg)
         Re: Perl array question - (admin) - (7)
             woooo.... - (folkert)
             "other"? - There's another? -NT - (broomberg) - (5)
                 Yes. Ruby. -NT - (ben_tilly) - (4)
                     No, that's the *other* other one. -NT - (admin) - (3)
                         You like TCL?? -NT - (ben_tilly) - (2)
                             Not without a ring, Mister. -NT - (admin) - (1)
                                 You prefer to take DOS to bat? -NT - (ben_tilly)

We have a moral imperative.
163 ms