Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

keeping split deliimiters causing empty string elements

by previous (Sexton)
on Apr 15, 2016 at 13:05 UTC ( [id://1160520]=perlquestion: print w/replies, xml ) Need Help??

previous has asked for the wisdom of the Perl Monks concerning the following question:

How can improve the regex to remove the double commas seen. Thank you in anticipation.
my $eqn='$profit=$sales-$cogs'; my @wrds = split /(=)|(-)/, $eqn; #gives profit,=,,$sales,,-,$cogs my $wrds_str=join(",",@wrds); $wrds_str =~ s/,,/,/g; @wrds = split /,/, $wrds_str; print join ",",@wrds;

Replies are listed 'Best First'.
Re: keeping split delimiters causing empty string elements
by Athanasius (Archbishop) on Apr 15, 2016 at 13:15 UTC

    Hello previous,

    TMTOWTDI: Put the separator in a single capture group:

    use strict; use warnings; use Data::Dump; my $eqn = '$profit=$sales-$cogs'; my @wrds = split /(=|-)/, $eqn; dd \@wrds;

    Output:

    23:13 >perl 1596_SoPW.pl ["\$profit", "=", "\$sales", "-", "\$cogs"] 23:13 >

    See the final paragraph of split.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hello Athanasius Thanks very much for your help and solution.
Re: keeping split deliimiters causing empty string elements
by BrowserUk (Patriarch) on Apr 15, 2016 at 13:13 UTC

    print join ',', split '([=-])', '$profit=$sales-$cogs';; $profit,=,$sales,-,$cogs

    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.
      That's great thank's very much for your help and solution.
Re: keeping split deliimiters causing empty string elements
by AnomalousMonk (Archbishop) on Apr 15, 2016 at 14:26 UTC

    As BrowserUk and Athanasius have both shown, the best way to deal with the problem (in split and in regex matching in general) is not to have unmatched capture groups, which return undef, in the first place.

    Sometimes, you're just gonna have capture groups that don't and won't match. The Perl version 5.10 Extended Patterns  (?|pattern) "branch reset" operator is very handy for this:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $eqn = '$profit=$sales-$cogs'; my @wrds = split /(?|(=)|(-))/, $eqn; dd \@wrds; " ["\$profit", "=", "\$sales", "-", "\$cogs"]
    If you don't have access to 5.10 or in general need to remove undefined (or zero-length, or whatever...) elements from a list, there's always grep:
    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $eqn = '$profit=$sales-$cogs'; my @wrds = grep defined, split /(=)|(-)/, $eqn; dd \@wrds; " ["\$profit", "=", "\$sales", "-", "\$cogs"]

    Update: Revised wording; meaning unchanged.


    Give a man a fish:  <%-{-{-{-<

      Thank you very much indeed for the pointers...for further reading. That's extremely helpful.

Log In?
Username:
Password:

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

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

    No recent polls found