How to split a string separated by a dot in perl? (1287 Views)
Reply
Valued Contributor
Joel Shank
Posts: 135
Registered: ‎04-14-1997
Message 1 of 9 (1,287 Views)
Accepted Solution

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

[ Edited ]

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

Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 2 of 9 (1,287 Views)

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...
Valued Contributor
Joel Shank
Posts: 135
Registered: ‎04-14-1997
Message 3 of 9 (1,287 Views)

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.
Esteemed Contributor
Abdul Rahiman
Posts: 232
Registered: ‎03-23-2004
Message 4 of 9 (1,287 Views)

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
Valued Contributor
Joel Shank
Posts: 135
Registered: ‎04-14-1997
Message 5 of 9 (1,287 Views)

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
Valued Contributor
Paul F. Carlson
Posts: 64
Registered: ‎05-19-2003
Message 6 of 9 (1,287 Views)

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

($first, $last) = split(/\./, $string);
Link down -- cable problem?
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 7 of 9 (1,287 Views)

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
Esteemed Contributor
Abdul Rahiman
Posts: 232
Registered: ‎03-23-2004
Message 8 of 9 (1,287 Views)

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
Occasional Advisor
meetlesli
Posts: 8
Registered: ‎06-29-2014
Message 9 of 9 (852 Views)

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

Thanks for example

Thanks,
Leslie
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.