in reply to regex behaves differently in split vs substitute?

raygun:

Re-read the perldoc -f split documentation. Since you have a grouping parenthesis in your regex, split is inserting the captured values into the list of values returned. Since '-' is a valid delimiter, and is outside the grouping parenthesis, it's inserting a null string into your list of values.

$ cat 930260.pl #!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { chomp $line; my @flds = split /-(?=[^-]+(-r[0-9]+)?$)/, $line; print join("|", @flds),">\n"; } __DATA__ mono-basic-2.10 mono-2.10.2-r1 mono-2.10.5 $ perl 930260.pl Use of uninitialized value $flds[1] in join or string at 930260.pl lin +e 9, <DATA> line 1. mono-basic||2.10> Use of uninitialized value $flds[3] in join or string at 930260.pl lin +e 9, <DATA> line 2. mono|-r1|2.10.2||r1> Use of uninitialized value $flds[1] in join or string at 930260.pl lin +e 9, <DATA> line 3. mono||2.10.5>

...roboticus

When your only tool is a hammer, all problems look like your thumb.

$ cat 930260.pl