in reply to Trimming whitespaces methods

Or take advantage of split's default behaviour
my @arr = (q{ one }, q{ two three }); for (@arr){ my $trimmed = join q{ }, split; printf qq{*%s*\n}, $trimmed; }
*one* *two three*

Replies are listed 'Best First'.
Re^2: Trimming whitespaces methods
by johngg (Canon) on Jun 30, 2008 at 15:37 UTC
    Your method seems to be the quickest so far :-)

    Rate capture alternation twoStage twoStageComma + splitJoin capture 8.93/s -- -27% -51% -52% + -66% alternation 12.2/s 36% -- -33% -34% + -53% twoStage 18.2/s 104% 49% -- -2% + -31% twoStageComma 18.6/s 108% 52% 2% -- + -29% splitJoin 26.2/s 193% 115% 44% 41% + --

    It is also compacting multiple spaces within the string, which is a side-effect you might not want.

    Cheers,

    JohnGG

      If you try the same benchmark with the + modifier instead of the * modifier you will see which is truely fastest:

      Rate alternation* capture alternation+ twoStage* twoS +tageComma* splitJoin twoStage+ twoStageComma+ alternation* 29.8/s -- -1% -18% -34% + -35% -44% -65% -66% capture 30.2/s 1% -- -17% -33% + -34% -43% -64% -66% alternation+ 36.4/s 22% 20% -- -20% + -21% -31% -57% -59% twoStage* 45.3/s 52% 50% 24% -- + -1% -15% -46% -48% twoStageComma* 45.9/s 54% 52% 26% 1% + -- -13% -46% -48% splitJoin 53.1/s 78% 76% 46% 17% + 16% -- -37% -40% twoStage+ 84.6/s 184% 180% 132% 87% + 84% 59% -- -4% twoStageComma+ 87.8/s 194% 191% 141% 94% + 91% 65% 4% --