I put a single line of text, beginning with "* [", and followed by arbitrary characters, into a $scaler variable.
split(
expr) splits it into the two sections "* [", and the rest of the line.
When I put the split output into an @array and print it with foreach, I get more out than I thought I put in. (a blank element at the beginning),
I'm sure I'm doing something stupid but for the life of me can't figure out what it is.
use warnings;
use strict;
my @MatchFoundArray = "";
my $WikiHistory ="* [Any garbled crap2345n&%]i";
print "WikiHistory contains:$WikiHistory\n"; # sanity check
@MatchFoundArray = split(m/(\* \[)/, $WikiHistory );
my $indexnum = 0;
foreach my $MatchFoundArray (@MatchFoundArray) {
print "Item", $indexnum++," is $MatchFoundArray\n";
}
The output looks like this:
WikiHistory contains:* [Any garbled crap2345n&%]i
Item0 is
Item1 is * [
Item2 is Any garbled crap2345n&%]i
So where the heck did Item0 come from????
2006-08-11 Retitled by planetscape, as per Monastery guidelines
( keep:2 edit:25 reap:0 )
Original title: 'I'm splitting a scaler and putting the results in an array. Why is the first element empty?'
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.