I am doing a little parsing, and I want to do a multi-pass split on a piece of data. The first split will be on whitespace, on the idea that anything separated by whitespace is guaranteed to be a separate token. However, tokens may also be not separated by whitespace (ooh, the abuse to grammar). For example, if my input were:

"x:= y + z;" <-- gotta love the pascal reference

Then the tokens woule be x, :=, y, +, z, and ;. (Btw, I'm not actually parsing pascal, but I just wanted to use something most people would probably recognize/understand).

But if I split on whitespace, the contents of my array would be:

( "x:=", "y", "+", "z;" )

So I need to run a second split on each element of the array, in such a fashion that it will split the letters/numbers from the punctuation (or at least I think that's what I need to do, but I'm not dedicated to the idea philosophically--if someone comes up with a one pass solution, I'll be perfectly happy to use it).

A few wrinkles
  1. Since this is a parse, I need to preserve the order. "x" must come before ":=", but both must stay before "y".
  2. I need to be able to specify the specific types of punctuation I want to split on. So hello(there) must become "hello", "(", "there", ")", but hello/there/local/bin must remain a single token. If anyone knows how I can pull this off, I will be highly impressed.

Thank you all yet again.

Some people drink from the fountain of knowledge, others just gargle.


In reply to Splitting inside an array by DeusVult

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.