Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

How to split a string separated by a dot in perl?

SOLVED
Go to solution
Highlighted
Joel Shank
Valued Contributor

How to split a string separated by a dot in perl?

I have a string, say, First.Last and want to split it on the dot. I use the following perl code:
($First,$Last) = split "." , $string;
I have also used:
($First,$Last) = split '.', $string;
And:
($First,$Last) = split /./, $string;
And have put parens around it:
($First, $Last) = split (".", $string);
But I get nothing! If the string is separated by any other character (comma or slash) it works! How can I split on a dot?

Thanks for your help!
jls

 

 

P.S. This thread has been moved from HP-UX>System Administration to  HP-UX > languages. -HP Forum Moderator

8 REPLIES
Mel Burslan
Honored Contributor

Re: How to split a string separated by a dot in perl?

assuming you are talking about perl, with my limited perl knowledge, . character is considered as wildcard and could be interpreted in unexpected ways. For instance a. can be used to present :

a34
apple
at
...
and so on.

so your dot may be getting interpretted as any character. Did you try escaping it with a backslash "\" ?
________________________________
UNIX because I majored in cryptology...
Joel Shank
Valued Contributor

Re: How to split a string separated by a dot in perl?

Yes, I have also tried escaping it:
($First,$Last) = split "\.", $string;

It still does not work.
Abdul Rahiman
Esteemed Contributor

Re: How to split a string separated by a dot in perl?

Have you tried using single quotes,
($First,$Last) = split '\.',"a.b"; seem to be working for me.
No unix, no fun
Joel Shank
Valued Contributor

Re: How to split a string separated by a dot in perl?

Yes, I've used single quotes. I should have indicated that I am using perl v5.6.0 for HPUX.

I am really at a loss here. I need to be able to split on the dot. I hope someone out there can help me...

jls
Paul F. Carlson
Valued Contributor
Solution

Re: How to split a string separated by a dot in perl?

($first, $last) = split(/\./, $string);
Link down -- cable problem?
harry d brown jr
Honored Contributor

Re: How to split a string separated by a dot in perl?

Paul's method works, as I use it all the time to split IP's (though with more variables on the left side). No points here please!

live free or die
harry
Live Free or Die
Abdul Rahiman
Esteemed Contributor

Re: How to split a string separated by a dot in perl?

($First,$Last) = split '\.',$string;
This works on HP-UX v11.11 and perl version 5.005_02
No unix, no fun
meetlesli
Occasional Advisor

Re: How to split a string separated by a dot in perl?

Thanks for example

Thanks,
Leslie