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

After all of the bugs, updates, and general messiness caused by this question, I started wondering if using split was really the right way to go. It can be done with a regex:

my @tests = ( '', qw( 2|3||||| 2 2| 2|3 2|3| | |2 |2| |2|3 |2|3| )); for my $string (@tests) { # Splits on '|' and preserves empty fields... my @fields = $string =~ m/((?:^|(?<=\|))[^|]*)/g; printf "%-10s :", "'$string'"; print "($_)" for @fields; print "\n"; } __END__ '' :() '2|3|||||' :(2)(3)()()()()() '2' :(2) '2|' :(2)() '2|3' :(2)(3) '2|3|' :(2)(3)() '|' :()() '|2' :()(2) '|2|' :()(2)() '|2|3' :()(2)(3) '|2|3|' :()(2)(3)()

However, constructing that regex wasn't particularly easy. I think, had I been faced with this task myself, I would have opted for the method you suggested. That is, append a non-empty field and then pop it off of the array after you split. At first it seems kind of a dirty trick but once you find how difficult it is to do it otherwise,

my @fields = split /\|/, $string.'|x'; pop @fields;
starts looking more and more elegant. I imagine it would score well on efficiency too.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re4: split/map weirdness: empty strings vs undef
by blakem (Monsignor) on Oct 05, 2002 at 09:02 UTC
    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