Eliminates all ws in each element of an array.

Title edit by tye

for (@arr) {s!\s!!g}

Replies are listed 'Best First'.
Re: whitespace eliminator for arrays
by Aristotle (Chancellor) on Oct 22, 2002 at 15:58 UTC
    You can get some more efficient by writing s/\s+//g for @array; Disregarding locales, the following is best: tr/\x09\x0A\x0C\x0D\x20//d for @array;

    Makeshifts last the longest.

      That also ignores Unicode. A slightly more complete substitution would be
      =~ s/[[:space:]]//g

      --
      perl -wpe "s/\b;([mnst])/'$1/g"

        Or, of course, s/[[:space:]]+//g; ;-)

        Makeshifts last the longest.

      Out of curiosity, why would specifiying the hex for each type of whitespace and using tr be better than a s///? Also, how does putting the for after the statement increase efficiency? Not that I'm nagging, I'm curious.

        You missed the little + I added to the regex. :-) Using for as expression modificator wasn't the point. The + will cause the regex to delete sequences of whitespace in one fell swoop, rather than character by character.

        tr is better in that it doesn't fire up the entire regex engine to do its job. It's therefor more efficient for the specific job of deleting all of a list of characters from a string. However, it doesn't understand classes like \s as the regex engine does, so you have to spell out all the characters. Doing so with hex codes is merely one choice out of several.

        Makeshifts last the longest.