twilliamsark has asked for the wisdom of the Perl Monks concerning the following question:
Given a source string of tags value pairs where the tags are represented by the character "A" and all text that is not "A" is considered to be the value associated with the tag.
The problem is to break up the source string into sub strings of TagValue.
So a source string of "A2ABA45" needs to be broken down to:
A2 AB A45
Here's my first attempt:
@parts=split(/(A.+?)(?=A|$)/,"A2ABA45"); $i=1; map {print $i++ . " -> $_\n"} @parts;
produces:
1 -> 2 -> A2 3 -> 4 -> AB 5 -> 6 -> A45
To get what I want I ended up with:
@parts=grep{$_}split(/(A.+?)(?=A|$)/,"A2ABA45"); $i=1; map {print $i++ . " -> $_\n"} @parts;
which produces:
1 -> A2 2 -> AB 3 -> A45
The questions are:
1) why the undef entries are there to begin with?
2) is there a better way?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: repeated regex capture
by kennethk (Abbot) on Apr 07, 2011 at 19:19 UTC | |
|
Re: repeated regex capture
by wind (Priest) on Apr 07, 2011 at 19:15 UTC | |
|
Re: repeated regex capture
by locked_user sundialsvc4 (Abbot) on Apr 07, 2011 at 23:22 UTC |