I've got a string inherited from an outside source that looks like this:
$data = "five[5]=12345&ten[10]=12345=678=90&six[6]=123456"
Now, the goal is to turn this into a name, value hash. I could do this like this:
my ($name, $value);
my @vars = split "&", $data;
foreach (@vars) {
($name, $value) = split "=";
$hash->{$name} = $value;
}
problem is, hash values can both have ampersands (&) and equal signs (=) in it, causing this to break. As you can see, each name is appended by the length index of it's value. I'm now trying to write a reg expression that will split this apart based on this length index:
while ($data =~ /(.*?)\[(\d+)\]=(.{[B]$2[/B]})/g) {
$hash->{$1} = $3;
}
However, this doesnt work because i cant figure out how to reference the length index (captured with $2) in the regex itself. The example i've given above where i simply included $2 as the length match doesnt work. also, each value still ends with an ambersand (&). If i were to include this into the regex it wouldnt match the last value which isnt followed by one. Anybody has an idea how to fix this or a more elegant way to do this. thank you.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.