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: Intermediate:
The Split Function

 



Nicolas
stranger

Nov 19, 2001, 7:26 AM

Post #1 of 3 (377 views)
The Split Function Can't Post

my $total = "98.23";
my @array = split(".", $total);

Why this part of code returns nothing in the array?

Thanks



mhx
Enthusiast / Moderator

Nov 19, 2001, 7:57 AM

Post #2 of 3 (376 views)
Re: The Split Function [In reply to] Can't Post

That seems like strange behaviour, but it's quite easy to explain. Wink

The first thing you need to know is that the first argument to split is treated as a regular expression. The dot in a regular expression means "any character". So you tell Perl to split the string "98.23" at any character. If you split the string at any character, you obviously end up with six empty fields. So you'd normally expect the splice function to return something like:

Code
@array = ('', '', '', '', '', '');

But, as the perldoc for splice says, the function deletes empty trailing fields before returning the list. Since all fields are empty, splice returns an empty list.

To make a long story short, what you want to do is:

Code
my @array = split /\./, $total;

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



Nicolas
stranger

Nov 19, 2001, 8:19 AM

Post #3 of 3 (374 views)
Re: The Split Function [In reply to] Can't Post

Thanks a lot Marcus, it works well!



 
 


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

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