in reply to regex verses join/split

Update: thanks to MeowChow and lemming, I've enrolled in an excellent course in which I will learn to read.

I have a comment to make about your first method, with the regexes. You do three distinct substitutions. That's just silly. The whitespace at the beginning of a string and at the end of a string is going to be treated like ALL the OTHER whitespace, so you're only being inefficient by separating the clauses. And, as I've already mentioned, doing s/\s+$// is particularly inefficient. So your regex-method should merely be s/\s+//g. But you can probably do better with tr/\n\r\f\t //d.

_____________________________________________________
Jeff japhy Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: regex verses join/split
by bwana147 (Pilgrim) on Jul 27, 2001 at 13:04 UTC

    Interesting, but the OP wanted to remove leading/trailing spaces, and collapse any sequence of whitespaces into one single space. You propose to remove any whitespace anywhere, which is not what was asked for.

    Still, it's true that tr/\n\r\f\t / /s would be more efficient than s/\s+/ /g. So, taking your remarks into account, I would say:

    s/^\s+//; 1 while s/\s$//; tr/\n\r\f\t / /s;

    --bwana147