I have a string that I want to split. But the separator is determined at runtime and so I need to pass it as a variable.
Something like my @fields = split(/$delimiter/,$string);
doesn't work. Any thoughts?
Input:
abcd|efgh|23
Expected Output:
abcd
efgh
23
You need to escape your delimiter, since it's a special character in regular expressions.
Option 1:
$delimiter = quotemeta($delimiter);
my @fields = split /$delimiter/, $string;
Option 2:
my @fields = split /\Q$delimiter/, $string;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With