473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Splitting a variable which contains '.' as the delimiter

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
3 4939
eWish
971 Expert 512MB
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
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
3,509 Expert Mod 2GB
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

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

Similar topics

1
by: Dennis Westermann | last post by:
I have the following problem XML-File: <SEARCH> <TAG>'x' or 'y'</TAG> </SEARCH> <CHARLIST> <CHAR>a</CHAR> <CHAR>x</CHAR>
7
by: qwweeeit | last post by:
Hi all, I am writing a script to visualize (and print) the web references hidden in the html files as: '<a href="web reference"> underlined reference</a>' Optimizing my code, I found that an...
8
by: Anthony Liu | last post by:
I have this simple string: mystr = 'this_NP is_VL funny_JJ' I want to split it and give me a list as 1. I tried mystr.split('_| '), but this gave me:
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
1
by: Gustav | last post by:
Hi! I use a regex (?<!\\?)('|\\+|:) to split a string to a String. The String i get after splitting is correctly splitted but contains all the delimiters i use to decide where the string...
5
by: mosscliffe | last post by:
I am looking for a simple split function to create a list of entries from a string which contains quoted elements. Like in 'google' search. eg string = 'bob john "johnny cash" 234 june' and...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
6
by: chrispwd | last post by:
Hello, I have a situation where I have a file that contains text similar to: myValue1 = contents of value1 myValue2 = contents of value2 but with a new line here myValue3 = contents of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.