What is a good way in Perl to split a line into pieces of varying length, when there is no delimiter I can use. My data is organized by column length, so the first variable is in positions 1-4, the second variable is positions 5-15, etc. There are many variables each with different lengths.
Put another way, is there some way to use the split function based on the position in the string, not a matched expression?
Thanks.
Yes there is. The
unpackfunction is well-suited to dealing with fixed-width records.Example
The first argument is the template, which tells
unpackwhere the fields begin and end. The second argument tells it which string to unpack.unpackcan also be told to ignore character positions in a string by specifying null bytes,x. The template'A4x2A9'could be used to ignore the"AB"in the example above.See
perldoc -f packandperldoc perlpacktutfor in-depth details and examples.