You want to extract variable length fields from your input.
split with a pattern matching the field
# given $RECORD with field separated by PATTERN, # extract @FIELDS. @FIELDS = split(/PATTERN/, $RECORD);
split function takes up to three arguments:
LIMIT parameter is
the maximum number of fields to split into. (If the input contains
more fields, they are returned unsplit in the final list element.) If
LIMIT is omitted, all fields (except any final
empty ones) are returned.
EXPRESSION gives the
string value to split. If
EXPRESSION is omitted,
$_ is split.
PATTERN is a
pattern matching the field separator. If
is omitted, contiguous stretches of whitespace are used as the field
separator and leading empty fields are silently discarded.
If your input field separator isn’t a fixed string, you might
split to return the field separators as well
as the data by using parentheses in
save the field separators. For instance:
returns the values:
(3, '+', 5, '-', 2)
To split colon-separated records in the style of the /etc/passwd file, use:
@fields = split(/:/, $RECORD);
The classic application of
@fields = split(/\s+/, $RECORD);
$RECORD started with whitespace, this last use
split would have put an empty string into the
first element of
split would consider the record to have ...