Or, you know, I could read the line just above that in the manual...

> If LIMIT is negative, it is treated as if it were instead arbitrarily large; 
> as many fields as possible are produced.

I just tried it, if you do "split(/\n/, $block, -1)", it does what you want.

Ricky

________________________________________
From: Boston-pm <boston-pm-bounces+remorse=mgh.harvard....@pm.org> on behalf of 
Morse, Richard E.,MGH via Boston-pm <boston-pm@pm.org>
Sent: Friday, May 29, 2020 2:26 PM
To: Greg London; boston...@mail.pm.org
Subject: Re: [Boston.pm] splitting on new lines is losing some lines?

        External Email - Use Caution

>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 <boston-pm-bounces+remorse=mgh.harvard....@pm.org> on behalf of 
Greg London <em...@greglondon.com>
Sent: Friday, May 29, 2020 1:51 PM
To: boston...@mail.pm.org
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
Boston-pm@pm.org
https://mail.pm.org/mailman/listinfo/boston-pm


_______________________________________________
Boston-pm mailing list
Boston-pm@pm.org
https://mail.pm.org/mailman/listinfo/boston-pm



The information in this e-mail is intended only for the person to whom it is
addressed. If you believe this e-mail was sent to you in error and the e-mail
contains patient information, please contact the Partners Compliance HelpLine at
http://www.partners.org/complianceline . If the e-mail was sent to you in error
but does not contain patient information, please contact the sender and properly
dispose of the e-mail.

_______________________________________________
Boston-pm mailing list
Boston-pm@pm.org
https://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to