in reply to Deciding unique elements of an array

Tsk,

Using a hash to create a unique array!

@array_thing = sort @array_thing; foreach (@array_thing) { push @unique if $_ ne $prev; $prev = $_; }
Could it be more simple...?

Sinister greetings.
"With tying hashes you can do everything God and Larry have forbidden" -- Johan Vromans - YAPC::Europe 2001
perldoc -q $_

Replies are listed 'Best First'.
Re^2: Deciding unique elements of an array
by rolfy (Acolyte) on Sep 14, 2005 at 01:21 UTC
    more simple - well.... yes and no...
    #!/usr/bin/perl my @a = ('a', 'b', 'c', 'c' ,'f', 'a', 'd'); my @b = grep (!$_{$_}++, @a);

    the 'my @b' line is the solution - it comes up with the correct info, but does involve doing a little work to get to the answer.

    It's definitely more simple to write :) but as for processing times etc - I really don't know if it's going to be better or not.

    It's a bit nasty to put this in your code without commenting it also, as I've got a few perl friends (none uber perl ppls, but 'know' perl), and none of them understood the intent :)