Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re4: split/map weirdness: empty strings vs undef

by blakem (Monsignor)
on Oct 05, 2002 at 09:02 UTC ( #203014=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: split/map weirdness: empty strings vs undef
in thread split/map weirdness: empty strings vs undef

Argh... I thought I could do better, but to get your exact results, all I could come up with was:
my @fields = $string =~ m/([^|]*)\|?/g; $string && $string !~ /\|$/ && pop @fields;
That second line can get a bit simpler if you define an empty string to have zero fields instead of having a single null field.

Congrats, your solution is better than mine even though it looks unnecessarily complicated.

I think the best suggestion so far is to tell split how many fields you want:

my @fields = split(/\|/,$string,$string =~ tr/|/|/ + 1);
Though I'd probably break it up into two lines:
my $fieldcount = $string =~ tr/|/|/ + 1; my @fields = split(/\|/, $string, $fieldcount);

-Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://203014]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-08-18 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (135 votes). Check out past polls.

    Notices?