What good is it in Perl to split a string into chunks of different lengths when there is no separator that I can use. My data is ordered by column length, so the first variable is at positions 1-4, the second variable is at positions 5-15, etc. There are many variables, each of which has a different length.
In other words, is there a way to use the split function, based on the position in the line, and not on matching the expression?
Thanks.
Yes there is. The unpack function is well suited for working with fixed-width records.
unpack
my $record = "1234ABCDEFGHIJK"; my @fields = unpack 'A4A11', $record; # 1st field is 4 chars long, 2nd is 11 print "@fields"; # Prints '1234 ABCDEFGHIJK'
The first argument is a template that tells unpack where the fields begin and end. The second argument indicates which line to unpack.
unpack can also be said to ignore character positions in a string by specifying zero bytes, x . The 'A4x2A9' can be used to ignore "AB" in the above example.
x
'A4x2A9'
"AB"
See perldoc -f pack and perldoc perlpacktut for details and examples.
perldoc -f pack
perldoc perlpacktut
Instead of using split try the method
split
You can use the substr() function to retrieve data by offset:
substr()
$first = substr($line, 0, 4); $second = substr($line, 4, 11);
Another option is to use a regex:
($first, $second) = ($line =~ /(.{4})(.{11})/);