Michael Rauh wrote at Fri, 05 Jul 2002 08:39:42 +0200:

> hi,
> 
> say i have an unsorted (large) list of, e.g., 0's and 1's:
> 
> @list = (0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, ...)
> 
> i now want to remove all the zeros, using the fastest way possible. how would U do 
>that?

When it is so important to use to _fastest_ way,
then you should take C or assembler.

> 
> i thought of sorting the zeros the left (or right) and then shift (or pop) all 
>zeros. or perhaps
> to first find the offset and then do a single splice.
> 
> but perhaps there's another way? performance is most important here. suggestions 
>welcome.

A very quick perl method is to do exactly what you had written.
Remove all zeros == take everything what's not a zero
and that's the exact behaviour of the grep function:

my @removed_zeros_list = grep $_, @list;

# Linguistic view:
#   the new list consists of all sensfull elements from the old list

(O.K. that removes every false value from the list -
 if there are also '' or undefs or similar that shouldn't be removed,
 you need something like 
   grep {$_ != 0}, @list;)

Best Wishes,
Janek


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to