I've looked at your solution, it works for a single pair
of backslashes at the end of a line, but it doesn't work
in the most general case i.e. any number of backslashes
at the end of one of the strings to be split. An odd
number of backslashes means that the ampersand is
backslashed, this is accounted for. However when there
are an even number of backslashes they will be reduced to
a single pair.
As far as I can see the following works in all cases
#!/usr/bin/perl -w
use strict;
my @split;
my $var;
# the original string that I want to split
my $tosplit = q(a=1&b=2\&3\&c=4\\\\\\\\\\\&d=5);
# print out the string to confirm how many backslashes have been left
# by the quote statement
print $tosplit . "\n\n";
# Split the string on the ampersand
my @temp = split /&/, $tosplit;
# Rejoin any strings that should not have been split
while ($_ = shift @temp) {
$_ .= ("&" . shift @temp) and redo if /[^\\]\\(\\\\)*$/;
push @split, $_;
};
# print the array so we can see the results
foreach $var (@split) {print "$var" . "\n"};
Baldrick, you wouldn't see a subtle plan if it painted
itself purple and danced naked on top of a harpsichord,
singing "Subtle plans are here again!"
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.