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

Need help on split-function

P: n/a
Hi All,

What I want to is using a string as PATTERN in a split function. This makes
it possible for me to change the PATTERN on one place in my script...

For example:
$separator = ";";
$line = "field1;value1";
local($field, $value) = split(/$separator/, $line);

How can I make this work ?

Arjen
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Arjen wrote:
What I want to is using a string as PATTERN in a split function. This
makes it possible for me to change the PATTERN on one place in my
script...

For example:
$separator = ";";
$line = "field1;value1";
local($field, $value) = split(/$separator/, $line);

How can I make this work ?


You don't tell us which part of your code is "not working". What is the
expected behaviour and how does it compare to the actual observed behaviour?
Without this information it is impossible to guess what you mean by "make it
work".

jue
Jul 19 '05 #2

P: n/a
Hi Jurgen,

You are right. I will give an example:

Code:
$line = "field1|value1";
$separator = "|";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = f
$value= v

Code:
$line = "field1^value1";
$separator = "^";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = field1^value1
$value=

Code:
$line = "field1;value1";
$separator = ";";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = field1
$value= value1

The last example is working right. The others are not. Even not when i'm
using "\|" instead of "|" for the seperator.

Arjen
"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:Y5*******************@nwrddc03.gnilink.net...
Arjen wrote:
What I want to is using a string as PATTERN in a split function. This
makes it possible for me to change the PATTERN on one place in my
script...

For example:
$separator = ";";
$line = "field1;value1";
local($field, $value) = split(/$separator/, $line);

How can I make this work ?
You don't tell us which part of your code is "not working". What is the
expected behaviour and how does it compare to the actual observed

behaviour? Without this information it is impossible to guess what you mean by "make it work".

jue

Jul 19 '05 #3

P: n/a
[Do not top post! Rearranged into chronological order]
Arjen wrote:
"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:Y5*******************@nwrddc03.gnilink.net...
Arjen wrote:
What I want to is using a string as PATTERN in a split function.
This makes it possible for me to change the PATTERN on one place in
my script...
[...] Code:
$line = "field1|value1";
$separator = "|";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = f
$value= v

Code:
$line = "field1^value1";
$separator = "^";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = field1^value1
$value=

Code:
$line = "field1;value1";
$separator = ";";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = field1
$value= value1

The last example is working right. The others are not. Even not when
i'm using "\|" instead of "|" for the seperator.


You got the right idea, but didn't follow through all the way.
Yes, | and ^ are special characters in a RE and need to be escaped with a
backslash when you want them to match the literal characters.

But when you define them in a double quoted string as in "\|" or "\^" then
Perl will notice that there is no special character in that string (those
characters are not special in strings, \t or \n are) and simply throw away
the backslash. That means "|" and "\|" are two different notations for the
same string.

What you want is a string that actually contains the backslash and then the
vertical bar.
Some solutions:
my $separator = "\\|";
my $separator = '\|';
my $separator = quotemeta ("|");

jue

Jul 19 '05 #4

P: n/a
Thanx for you replies !

Arjen

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:4J******************@nwrddc02.gnilink.net...
[Do not top post! Rearranged into chronological order]
Arjen wrote:
"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:Y5*******************@nwrddc03.gnilink.net...
Arjen wrote:
What I want to is using a string as PATTERN in a split function.
This makes it possible for me to change the PATTERN on one place in
my script...
[...]
Code:
$line = "field1|value1";
$separator = "|";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = f
$value= v

Code:
$line = "field1^value1";
$separator = "^";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = field1^value1
$value=

Code:
$line = "field1;value1";
$separator = ";";
local($field, $value) = split(/$separator/, $line);

Gives back:
$field = field1
$value= value1

The last example is working right. The others are not. Even not when
i'm using "\|" instead of "|" for the seperator.


You got the right idea, but didn't follow through all the way.
Yes, | and ^ are special characters in a RE and need to be escaped with a
backslash when you want them to match the literal characters.

But when you define them in a double quoted string as in "\|" or "\^" then
Perl will notice that there is no special character in that string (those
characters are not special in strings, \t or \n are) and simply throw away
the backslash. That means "|" and "\|" are two different notations for the
same string.

What you want is a string that actually contains the backslash and then

the vertical bar.
Some solutions:
my $separator = "\\|";
my $separator = '\|';
my $separator = quotemeta ("|");

jue

Jul 19 '05 #5

P: n/a
What you want is to split on a literal string. The split function
uses a regular expression, thus when you use separators with special
characters such as '|' or the like you will get splits that may
appear different to the results you expected.

Short answer, replace the split function with:
split( /\Q$separator\E/, $line );

The \Q...\E combination forces the regular expression to search
for quoted text that won't be interpreted as a regular expression
(I know that doesn't make sense, but just do it an it will work).

Regards,
Peter

Arjen <mi**********@msn.com> wrote:
What I want to is using a string as PATTERN in a split function. This makes
it possible for me to change the PATTERN on one place in my script... For example:
$separator = ";";
$line = "field1;value1";
local($field, $value) = split(/$separator/, $line);

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.