How do you tokenise / tokenize / split a delimited string in Perl?
Question
How do you split a string e.g. "a:b:c:d" into tokens for parsing in Perl?
(e.g. using split?)
Looking for clear, straightforward answer above all (but do add any interesting tidbits of info afterwards).
Solution
Yes, split is what you want.
@tokens = split(/:/, "a:b:c:d");
foreach my $token (@tokens) {
....
}
OTHER TIPS
You can use split. You can also use it with a regex.
my @tokens = split(/:/,$string);
For more advanced parsing, I recommend Parse::RecDescent
Also take a look at the documentation that comes with perl by typing at a command line prompt:
perldoc -f split
To search the FAQs use
perldoc -q split
if you have:
$a = "a:b:c:d";
@b = split /:/, $a;
then you get:
@b = ("a", "b", "c", "d")
In general, this is how split works:
split /PATTERN/,EXPR
Where PATTERN
can be pretty much regex. You're not limited to simple tokens like ':
'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow