meanroy has asked for the wisdom of the Perl Monks concerning the following question:

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?'

  • Comment on I'm splitting a scalar and putting the results in an array. Why is the first element empty?
  • Select or Download Code

Replies are listed 'Best First'.
Re: I'm splitting a scalar and putting the results in an array. Why is the first element empty?
by ikegami (Patriarch) on Aug 10, 2006 at 21:52 UTC

    The specified regexp is used to locate the seperator. split returns the two items being seperated by the seperator (as well a the seperator because of the parens). In this case, that's the string "" and "Any garbled crap2345n&%]i".

    Regexps are more apt at extracting data when there is no clear, repeating seperator.

    @MatchFoundArray = $WikiHistory =~ /(\* \[)(.*)/;
      Oh DUH! Most Excellent!
      Both instant answers brought me enlightenment, Thanks a bunch!
Re: I'm splitting a scalar and putting the results in an array. Why is the first element empty?
by BrowserUk (Patriarch) on Aug 10, 2006 at 21:53 UTC

    split produces a list of the things either side of the match. If the match occurs at the very start of the data, then a null field is produced. This is Item0.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: I'm splitting a scalar and putting the results in an array. Why is the first element empty?
by GrandFather (Saint) on Aug 10, 2006 at 22:00 UTC

    You have a seperator sequence so there must be something to the left of it - even if it is very short :). NOte that there doesn't have to be anything after. Consider:

    use warnings; use strict; my $test =",124,456,7890"; print "Test string: >$test<\n"; my @matches = split m/(,)/, $test; print "Item $_ is >$matches[$_]<\n" for 0..$#matches;

    Prints:

    Test string: >,124,456,7890< Item 0 is >< Item 1 is >,< Item 2 is >124< Item 3 is >,< Item 4 is >456< Item 5 is >,< Item 6 is >7890<

    DWIM is Perl's answer to Gödel
Re: I'm splitting a scalar and putting the results in an array. Why is the first element empty?
by bart (Canon) on Aug 11, 2006 at 09:21 UTC
    Not that this has any effect on your code, as you later plainly assign a list to your whole array, but don't do this:
    my @MatchFoundArray =  "";
    unless you really want to end up with an array containing a string as the single item.
      I added that while (attempting) troubleshooting the problem to make sure I knew what was in the array to begin with.
      I'm trying to do some fairly sophisticated stuff with regex, and frankly, in way over my head at this point.
      FWIW the responses I got here led me to re-read the entries in the Llama and Camel books on regex, followed by an entire day of starting from the beginning (again!) and jumping around trying out examples in the Owl book.
      At least now I'm making a little progress.
      Thanks again.
      Roy.
Re: I'm splitting a scalar and putting the results in an array. Why is the first element empty?
by Moron (Curate) on Aug 11, 2006 at 14:13 UTC
    The explanations are all very interesting and believable, but should equally apply to say version 5.0005 of Perl. Unfortunately they don't -- on a whim, I ran something similar against Perl 5.0005 running on an old Solaris box, and the code DWIMed relative to the OP, i.e. the '\* [' separator did not get entered into the array returned by split. This suggests to me that yet something else is afoot.

    -M

    Free your mind

      I think they apply, unless I am mistaken. Granted, english is not my first language, so I may have misunderstood your post, but it is a normal behaviour for split not to enter the separator in the returned array.
      For example, this bit of code :
      my $string = "aa:bb:cc:dd:ee:ff:gg:hh"; my @table = split /:/ , $string; print "@table\n";
      would print
      aa bb cc dd ee ff gg hh
        It does if you have parans in the regex.
        my @table = split /(:)/, $string
        --Artist