I am trying to understand split and its documentation. To do this I have implement split by using m{} and the variables @- and @+.
By this I have found some problems
This script shows some of them:
use strict;
use warnings;
use 5.010;
use Data::Dump qw(dump dd ddx);
sub split_e { }
split_e( // );
# Warning: Use of uninitialized value $_ in pattern match (m//) at pm_
+1.pl ...
my @rv = split( //, '' );
# Warning: none
my $str = '1-10,20';
my $pat = '(-)|(,)';
@rv = split( $pat, $str );
warn dump @rv; # (1, "-", undef, 10, undef , ",", 20)
@rv = $str =~ m{$pat}g;
warn dump @rv; # ("-", undef, undef, ",")
while ( my $rv = $str =~ m{$pat}gc ) {
for my $ix ( 0 .. 99 ) {
if ( defined $-[$ix] ) {
say sprintf '$ix= %d (%d,%d)<%s>', $ix, $-[$ix], $+[$ix],
substr $str, $-[$ix], $+[$ix] - $-[$ix];
}
}
}
say "Strawberry Perl $^V";
say $^O;
__DATA__
output:
$ix= 0 (1,2)<->
$ix= 1 (1,2)<->
$ix= 0 (4,5)<,>
$ix= 2 (4,5)<,>
Strawberry Perl v5.30.1
MSWin32
Questions
Magic in split
This split_e( // ); gives the warning:
Use of uninitialized value $_ in pattern match (m//) at pm_1.pl ... but this
split( // ); does not.
Is there some magic in split indicate by the ‘/’s in “/PATTERN/ in the documentation?
Bug in the variables @- and @+
I find that the result from split, m{} and when using
$-[$ix] $+[$ix] are inconsistent.
Split gives (1, "-", undef, 10, undef , ",", 20).
m{} gives ("-", undef, undef, ",").
Using $-[$ix] $+[$ix] does not include the undefs
Is this a bug in the last case?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.