Re: [Boston.pm] splitting on new lines is losing some lines?
"Morse, Richard E.,MGH via Boston-pm" <[email protected]> Fri, 29 May 2020 18:28:35 +0000
| Newsgroups | gmane.comp.lang.perl.perl-mongers.boston |
|---|---|
| Message-ID | <CY4PR04MB0729F019663D139275905C80E78F0@CY4PR04MB0729.namprd04.prod.outlook.com> |
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.edu-aE07yma9hCw@public.gmane.org> on behalf of Morse, Richard E.,MGH via Boston-pm <[email protected]> Sent: Friday, May 29, 2020 2:26 PM To: Greg London; [email protected] 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.edu-aE07yma9hCw@public.gmane.org> on behalf of Greg London <email-2Ro/Dj86MvDSUeElwK9/[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 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.