Welcome to WebmasterWorld Guest from 54.159.26.69

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

consolidate duplications in an array? (perl)

     

littleman

3:05 am on Feb 28, 2001 (gmt 0)

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



How would you all consolidate duplications in an array?
from:
@array = qw( 1 2 3 3 4 5 6 7 7 8 )
to:
@array = qw( 1 2 3 4 5 6 7 8 )

sugarkane

11:18 am on Feb 28, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The standard way is this:

foreach $i (@array) {
push @new_array, $i unless $tmp{$i}++;
}

For fun, I also tried this:

foreach $i (@array) {
$tmp.="$i " unless $tmp=~/$i/;
}
@new_array=split(" ",$tmp);

...but it's waaay slower.

littleman

11:21 pm on Feb 28, 2001 (gmt 0)

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Thank you Sugarekane. I actually was doing something along the lines of your second approach. I like the first approach much better.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month