Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Do I have to trick Split?

by $h4X4_|=73}{ (Monk)
on Aug 20, 2016 at 12:12 UTC ( [id://1170123]=perlquestion: print w/replies, xml ) Need Help??

$h4X4_|=73}{ has asked for the wisdom of the Perl Monks concerning the following question:

I found out something strange about split. I guess it will not parse blank content till there is something at the end...
I have a string that splits at a comma,but when there are repeated delimiters at the end they will not be added to the array till there is something at the end like so...

#!perl use warnings; use strict; use Data::Dumper; my $str = "ABC,123,,,,,,"; # just ABC and 123 #my $str = "ABC,123,,,,,,this"; # now it shows the blanks. my @elems = split ',', $str; print Dumper \@elems;
Any other ways maybe regex or should I trick the array and then pop the last content out?

Replies are listed 'Best First'.
Re: Do I have to trick Split?
by BrowserUk (Patriarch) on Aug 20, 2016 at 12:17 UTC

    Use the third parameter to split. From the docs:

    If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of pop would do well to remember). If LIMIT is negative, it is treated as if an arbitrarily large LIMIT had been specified.

    If you want to retain all empty trailing fields, specify -1 as the third parameter.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Well that's awesome. I have this problem a lot, and I've always written around it since I didn't know you could specify a negative limit and get all the trailing empties. As usual I didn't RTFM well enough.

      Thanks for this tip. I feel pretty silly now for how many times that, among other things, I've added extra no warnings 'uninitialized'; and use warnings 'uninitialized'; around every split in to an array where I have that problem.

      I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
      I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious
        I have this problem a lot, and I've always written around it since I didn't know you could specify a negative limit and get all the trailing empties.

        Pretty much the defining characteristic of Perl -- the real Perl that is; not a bastardised, pseudo-sanitised subset, nor an orthoganised, pseudo-java superset that the "modern" crowd would try to sell you on -- is that the built-ins are very well designed to deal with all the variations that you will ever need.

        Many languages now have a split in their libraries, but none of them cater to all the common variations and edge cases the way Perl's does.

        It was when early in my use of Perl: I repeatedly found myself needing to do something that I knew was going to be awkward and messy to deal with, only to find that Perl's designer(s) had been there before me, recognised the need and catered for it; that caused me to pin the Perl documentation in my browser for quick reference.

        It's still there 13 years later. I don't have to look so often as I once did; but I am still discovering the purpose of stuff that I've read many times, but took no notice of because I didn't need it at that time, but that has been sitting there dormant just waiting for my need.

        And it is that that brings me back to Perl time and time again. I'm currently trailing yet another new language -- this time Julia -- to replace my reliance on Perl. It is very cool language that acts exactly as a RELP-able, interpreted language; that when stressed, JITs into a near-C speed demon. But I recently spent a week trying to code something from scratch in Julia; that when I got frustrated took me 37 minutes from start to finish in Perl.

        Even with the working Perl example and a 2/3rds complete Julia version it took 3 more days to get the Julia version working because of the need to manually code all the special cases that Perl takes care of for me. The result ran 13x faster, which in the long run of process that will run for months, is a huge saving and well worth the effort; but it will still be a long time before I give up Perl 5.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.

        perldoc -f split is your friend!

        As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1170123]
Approved by BrowserUk
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found