[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