Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I split a string of characters into two values?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Please help.

I need to split a string known as $an{'example'} into two values.

e.g.

$an{'example'} = cat.com

I need to make

$part1 = cat
$part2 = .com

I was thinking something on the lines of;

$part1 =$an{'example'} =~ s/[\W]...//g;
$part2 =$an{'example'} =~ s/^(\w+?)$//g;

My usage of = in the second instance of each line above is obviously wrong (I'm hoping you can see the result I'm trying to achieve).
Any help is greatly appreciated.

Be easy on me I'm only a beginner ! :)

Thank you
 
use the split command:

($firstword, $secondword) = spilt(/./, $an{'example');

this splits the string on the . delimiter..
then print out the variables

print "$firstword" etc...
 
NoviceBloke

Did Jimberger answer your question? Is your string going to have '.' all the times. From my understanding, you probably want to have this..
($firstword, $secondword) = spilt(/./, $an{'example');
$part1 = $firstword
$part2 = '.' + $secondword

Regards
 
Thanks Jimberger
Thanks TechnicalAnalysis

$part1 = $firstword
$part2 = '.' + $secondword

is exactly what I wanted to achieve.
Thanks again



now..... it's back to the Perl manual for me :)

 
Be careful using "."(dot) in commands that use regular expressions, because "." is a special character and it means to match any single character. So if you want to match a true dot(or period), then you should escape the dot with a backslash like this:

($firstword, $secondword) = spilt(/\./, $an{'example');

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Ooops !

Did I say...

>$part2 = '.' + $secondword
>is exactly what I wanted to achieve.

No, this does not appear to put . in front of $secondword

. is lost but this successfully splits;

($firstword, $secondword) = split /\./, $an{'example'};


Thanks again for all your help.

 
yes, the dot must be quoted for work on a regular expression.

also, the concatenation string operator in perl is "."
and not "+" (the later is true for java or python).

So, you need this:
$part2 = '.' . $secondword;

Also, a more direct way to do it, might be:
($part1, $part2) = $an{'example'} =~ /(.*)(\..*)/;


regards,

--
pkiller



 
You might want to make that:
Code:
($part1, $part2) = $an{'example'} =~ /(.*?)(\..*)/;
Otherwise, the first .* is "greedy", and will gobble up everything, so the second part might never match. Adding the ? after the .* makes it "non-greedy", and it will only take as much as necessary to make the regex match.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Tracy, you are not correct.
Your RE will left on $part1 upto the first dot,
not including it. My RE will left on $part1 upto
the last dot, not including it.

If there is a dot on the searched expression, both
REs will always left something on $part1 and $part2.

The original problem, was examplified with one dot
only, but usually, people want to take the extension
of something, thus, globbing until the last dot. That's
what my RE does.

cya

--
pkiller
 
pkiller: You're correct, of course. If you want everything but the last dot to go into the first part, your regex was correct. It depends on what you want to do. In this case, yours was the correct way. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top