>From the documentation for `split`: > If LIMIT is omitted (or, equivalently, zero), then it is usually treated as > if it were instead negative but with the exception that trailing empty fields > are stripped (empty leading fields are always preserved); if all fields are > empty, then all fields are considered to be trailing (and are thus stripped > in this case).
It's a documented feature; my guess is that this was the behavior of `awk`, and this matches that? But it may also have been a conscious decision, in that most people working with text don't care about extra empty fields at the end — and if they do, they can specify how many fields they want. It's a bit more work, but you could split on `/(\n)/` instead, which will return the split character as separate entries, but will also include any blank entries at the end. (You could filter the split character out with `grep`/`map` fairly easily...) Ricky ________________________________________ From: Boston-pm <[email protected]> on behalf of Greg London <[email protected]> Sent: Friday, May 29, 2020 1:51 PM To: [email protected] Subject: [Boston.pm] splitting on new lines is losing some lines? External Email - Use Caution I feel like I'm losing my mind on this. Why would perl do this? This script: #!/bin/env perl use warnings; use strict; use Data::Dumper; my $block=<<'BLOCK'; alpha bravo charlie delta BLOCK ; print "block is:vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n"; print $block; print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"; my @lines = split(/\n/, $block); print "when I split on \\n, I get this:\n"; print Dumper \@lines; OUTPUT OF SCRIPT IS: block is:vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv alpha bravo charlie delta ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ when I split on \n, I get this: $VAR1 = [ '', 'alpha', 'bravo', '', '', 'charlie', 'delta' <======== SHOULD BE SOME BLANK ENTRIES AFTER DELTA ]; _______________________________________________ Boston-pm mailing list [email protected] https://mail.pm.org/mailman/listinfo/boston-pm _______________________________________________ Boston-pm mailing list [email protected] https://mail.pm.org/mailman/listinfo/boston-pm

