Need help on split-function

A

Arjen

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
 
J

Jürgen Exner

Arjen said:
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
 
A

Arjen

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

Jürgen Exner

[Do not top post! Rearranged into chronological order]
Arjen said:
[...]
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
 
A

Arjen

Thanx for you replies !

Arjen

Jürgen Exner said:
[Do not top post! Rearranged into chronological order]
Arjen said:
Jürgen Exner said:
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
 
P

P Payne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top