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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
why split failed

 



babyBlue
Novice

Nov 2, 2004, 5:46 PM

Post #1 of 3 (603 views)
why split failed Can't Post

Hi all,

I did a split by @numbers = split(".", "1.2.3.4); but the @numbers is always null. Can someone help? Thanks.


KevinR
Veteran


Nov 2, 2004, 5:57 PM

Post #2 of 3 (602 views)
Re: [babyBlue] why split failed [In reply to] Can't Post

if this is the code you are using:

@numbers = split(".", "1.2.3.4);

its missing an ending double quote after the string you are wanting to split. Also, the double quotes around the dot seem to be messing up the pattern matching, even when the dot is escaped "\." although I am not sure why that is. You can write it like this and I am sure it will work:

@numbers = split(/\./, "1.2.3.4");
-------------------------------------------------


davorg
Thaumaturge / Moderator

Nov 4, 2004, 5:11 AM

Post #3 of 3 (596 views)
Re: [babyBlue] why split failed [In reply to] Can't Post

Just a couple of points to back up what KevinR has already told you.

Firstly, it's always worth looking at the documentation for any function that you are having. In this case, the docs for split make it clear that the first argument is interpreted as a pattern (also known as a regular expression), not a string. A dot has a special meaning in regular expressions - it means "match any character".

Even tho' the first parameter doesn't look like it, it is still interpreted as a regex. I never use quotes to delimit the first argument to split as it is too easy to forget and think that it is a string. If you had written the expression as

Code
@numbers = split(/./,  "1.2.3.4");

Then I think it would have been easier to see your error.

It's also worth looking at what exactly happened in your version of the code. The regex that split splits on is thrown away. For example iyou had code like:

Code
my @numbers = split /,/, '1,2,3,4';

Then you wouldn't expect to see any comma characters left in the elements of @numbers. They would have been thrown away.

It's the same with your code. You code says "split the string whenever you find any character and return a list of strings from between those characters". Now, by definition there can't be any characters left between "any character". So you get a list of "undef" values. By default Perl removes any trailing undefined elements from the list returned by split so you get an empty list.

It might be interesting to compare your code with what you get if you force Perl to return all of the elements in the list (by giving split a negative third parameter).


Code
# Based on your code - prints nothing 
print map { "<$_>" } split /./, '1.2.3.4';

# Forcing all empty elements
print map { "<$_>" } split /./, '1.2.3.4', -1;


In the second example you get a number of empty <> sequences printed out. One for each element in the return list.

Hope this helps. Let me know if you need any more detail on any of this.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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