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).
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).
You can use split. You can also use it with a regex.
my @tokens = split(/:/,$string);
For more advanced parsing, I recommend Parse::RecDescent
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 ':
'
Yes, split is what you want.
@tokens = split(/:/, "a:b:c:d");
foreach my $token (@tokens) {
....
}