http://www.perlmonks.org?node_id=287570


in reply to Tutorial suggestion: split and join

Regular expressions are used with the split function to break up strings into a list of substrings. The converse to split is the join function, which takes a list of strings and joins them together again.
No. The converse to a split is a regular expression (with matching). The idea here is that with matching you specify what you want to match and you get that as a return value ala @simpsons = "Bart Lisa Maggie Marge Homer" =~ /(\w+)/g). With split you specify all the things you don't want and you get everything else. Which one you use depends on what is more natural to specify. I'm glossing over the fact that plain regular expressions are actually even more powerful than that but that's how they compare with split anyway (which itself uses a regex as its first parameter).

If split is given a null string as a delimeter, it splits on each character in the string, and since the delimeter is nothing at all, nothing at all is thrown away.
No. The empty regular expression matches at every position possible which includes between characters. This is how anchoring works. As an example, ^ normally matches the position before the first character. This is only possible if the "spaces" before, after and between characters are also places to match. So when matching "ab" with // it can match before the "a", the "a", between the "a" and the "b" and after the "b". When you actually run that the only parts that get returned in the split is the "a" and the "b".

The word "delimit" isn't spelled with an 'e' in it.

Replies are listed 'Best First'.
Re: Re: Tutorial suggestion: split and join
by zby (Vicar) on Aug 29, 2003 at 07:47 UTC
    The converse to split is the join function, which takes a list of strings and joins them together again.

    No. The converse to a split is a regular expression (with matching). The idea here is that with matching you specify what you want to match and you get that as a return value ala @simpsons = "Bart Lisa Maggie Marge Homer" =~ /(\w+)/g). With split you specify all the things you don't want and you get everything else. Which one you use depends on what is more natural to specify.

    I don't understand what meaning of converse you use here. English is not my first language - but here is what I found for 'converse' in the Meriam-Webster online dictionary:
    something reversed in order, relation, or action: as a : a theorem formed by interchanging the hypothesis and conclusion of a given theorem b : a proposition obtained by interchange of the subject and predicate of a given proposition <"no P is S " is the converse of "no S is P ">
    I believe the OP used 'converse' as a synonim for 'inverse' in the mathematical sense, i.e. he wanted to say that  split o join = identity. Which is quite close to being correct.

    Or perhaps you meant join in the first sentence? This possibillity occured to me just after posting this comment.

Re: Re: Tutorial suggestion: split and join
by davido (Cardinal) on Aug 29, 2003 at 02:35 UTC
    Thank you for the information and clarification. I've re-worked the appropriate portions of the node and provided credit at the end.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      The 'delimet' (sic) spelling error still persists in a heading.

      Cheers!

      Update: I see you fixed the heading but the error also still exists in '...same thing as specifying no delimeter at all' and in the credits section.

        More nit-picking: You may want to consider a spell check next time since you also incorrectly spelt the following (correct spellings shown): preceding, boundary, boundaries, parenthesis and behaviour.

        Cheers!