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