CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
need to split without ignoring zero-length strings



Dec 13, 2000, 8:07 AM

Post #1 of 2 (215 views)
need to split without ignoring zero-length strings Can't Post

How do I make the following line not ignore zero-length strings? Or if someone has a procedure that does this, that would be great!!

@otherchoices = split(/\|/, $choices);

I have a $choices= '|ASDF|ERTY|SDFGH' and I need: "", "ASDF", "ERTY" and "SDFGH"

The Python split does what I want, but that isn't an option, unless you know if perl can call python. How would I do that? Putting a space isn't an option either, it needs to be a zero-length string.

Where can I go to find information?

Thanks for responding!!


Dec 13, 2000, 9:31 AM

Post #2 of 2 (211 views)
Re: need to split without ignoring zero-length strings [In reply to] Can't Post

I am not sure why your code does not work. It looks perfectly normal... May be you are looking at a wrong place at the right time? :)


$choices = "|A||E|C|D";
print "[$_]\n" for split /\|/, $choices;

The code prints:


As you can see, it works as expected.

Take a look at this example

$choices = "|A||E|C|D|";
print "[$_]\n" for split /\|/, $choices;

This code has exactly the same output as the previous one.
It may seem that split() ignores the very last field if it is empty.
In fact, it doesn't. The last pipe character is interpreted as the field terminator for "D", and not as the beginning of the next field.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives