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

Hello! Can someone tell me, why this code doesn't provide me the right result? I would like to split the $text at "</Text>", but it seems to be, that the regex can't find the search expression.
my $text="ABCDE<\/Text>TUVWX"; my @splitted=[]; @splitted=split("\<\/Text\>".$text); print "0.".$splitted[0]."\n"; print "1.".$splitted[1]."\n";

Replies are listed 'Best First'.
Re: Problem with split
by Corion (Patriarch) on Jun 01, 2018 at 09:04 UTC

    If you allow Perl to tell you about things it considers weird, you'll get more information:

    use warnings; my $text="ABCDE<\/Text>TUVWX"; my @splitted=[]; @splitted=split("\<\/Text\>".$text); print "0.".$splitted[0]."\n"; print "1.".$splitted[1]."\n"; __END__ Use of uninitialized value $_ in split at tmp.pl line 7.

    This is because you only pass one argument to split, instead of two arguments.

    One argument is because of the dot instead where you would want to write a comma:

    @splitted=split("\<\/Text\>".$text); # ^

    Also, the first argument to split is a regular expression, not a string. You might want to change your code to accomodate that:

    @splitted=split(m!\<\/Text\>!, $text);
      Thank you Corion the fast response!
Re: Problem with split
by dave_the_m (Monsignor) on Jun 01, 2018 at 09:07 UTC
    You have a dot (concatenation operator) after the pattern, rather than a comma.

    PS: note that split takes a pattern rather than a string as the regex; if you supply it with a string, that string undergoes normal double-quoted string processing, before belated being passed to the regex engine, which can cause things like \b to be misinterpreted. So get into the habit of using pattern delimiters:

    @splitted=split(/\<\/Text\>/, $text);

    PPS: my @splitted=[]; doesn't do what you think it does. It is setting @splitted to contain one element, which is a reference to an array. You probably meant my @splitted= (). But that is doubly redundant; firstly the my declaration already creates an empty array, and secondly you immediately assign a new value to the array anyway - the returned list from split - so it doesn't matter if the array wasn't empty before.

    Dave.

Re: Problem with split
by LanX (Saint) on Jun 01, 2018 at 09:08 UTC
    Use a a comma not a dot.

    The dot is concatenating strings, you want to separate two arguments.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Problem with split
by tuby (Novice) on Jun 01, 2018 at 09:07 UTC
    I was so stupid, I made a typing error. I was using a dot instead of comma.
      Don't feel bad about it. By asking a coherent question and providing code you have contributed to the wealth of knowledge about Perl! The next time someone makes the same mistake they may find this thread and be very grateful.
        Finding this thread is not easy, because the problem is complex and hard to describe. Sometimes it is easier to make a new thread.
Re: Problem with split
by tuby (Novice) on Jun 01, 2018 at 08:58 UTC
    Perl Version
    This is perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x86-multi-thread-64int