in reply to Array Cleaning

Maybe I'm missing something, but why are you not using grep? E.g.

@array = (3, 2, 1, "", 2, undef); @cleanarray = grep { defined $_ && $_ ne "" } @array; @sortedcleanarray = sort grep { defined $_ && $_ ne "" } @array;

Of course, this will not remove duplicates. From your post I am not sure whether you want that or not (Your "should be" output has the duplicates removed).

Replies are listed 'Best First'.
Re: Re: Array Cleaning
by Anonymous Monk on May 02, 2003 at 17:01 UTC

    Yes, I forgot to mention the duplicate entries. They should also be removed.

    Another problem with grep is, that it is extremely slow with big arrays (at least I heard that :))

      1. Never guess wildly at what may or may not be slow.
      2. Don't concern yourself with performance unless it is a problem.
      3. When you need performance, profile your code to find out where the problem is.
      4. If you really really need a very very fast program then why are you using Perl?
      And some quotes from the collection on my home node:
      More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity.
      — William A. Wulf, A Case Against the GOTO
      Rules of Optimization:
      Rule 1: Don't do it.
      Rule 2 (for experts only): Don't do it yet.
      — Michael A. Jackson

      Makeshifts last the longest.

      Well, I find it hard to believe that a built-in perl function that has been optimised and is written in C should be slower than walking through the array yourself... (ie. doing the exact same thing the built-in is supposed to do). It is contrary to my own experience, at least. Plus I find myself too lazy to write and debug own code when I can use a built-in :).

      If you really have reason to believe what you've heard, why not just quickly whip up a benchmark.