By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,275 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,275 IT Pros & Developers. It's quick & easy.

Splitting a variable which contains '.' as the delimiter

P: 68
Hi,

I would like to seperate a variable which has '.' (dot) as the delimiter using the split function.
Expand|Select|Wrap|Line Numbers
  1. $var1 = "first.second";
  2.  
  3. @value = split(/./, $var1);
then the value of $value[0] should be "first" and $value[1] should be "second".

but this doesnt happen. it works fine with ',' (comma) as the delimiter but doesnt work with a dot.

can someone guide me here?

any help is appreciated.


Thank You,

Ravi
Jan 9 '08 #1
Share this Question
Share on Google+
3 Replies


eWish
Expert 100+
P: 971
You need to escape the dot with \. If there are elements already in the array then use push to add the elements to the array.
Expand|Select|Wrap|Line Numbers
  1. my @array;
  2. my $var = 'Hello.World';
  3.  
  4. push(@array, split(/\./, $var)); 
  5.  
  6. print $array[0];
  7. print $array[1];
--Kevin
Jan 9 '08 #2

P: 68
You need to escape the dot with \. If there are elements already in the array then use push to add the elements to the array.
Expand|Select|Wrap|Line Numbers
  1. my @array;
  2. my $var = 'Hello.World';
  3.  
  4. push(@array, split(/\./, $var)); 
  5.  
  6. print $array[0];
  7. print $array[1];
--Kevin
Thank you so much.
It worked.


Ravi
Jan 9 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Thank you so much.
It worked.


Ravi
Just to add to what Kevin said about escaping the '.', the reason you have to escape it is because in regular expressions (which is what is between the / and / when using split), the dot has the special meaning of "any single character". So, you have to remove that meaning and make it a character itself by escaping it.

Regards,

Jeff
Jan 9 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.