Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read chunks of data in Perl

Tags:

perl

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.

like image 556
itzy Avatar asked Jun 29 '10 20:06

itzy


3 Answers

Yes there is. The unpack function is well-suited to dealing with fixed-width records.

Example

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 the template, which tells unpack where the fields begin and end. The second argument tells it which string to unpack.

unpack can 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 pack and perldoc perlpacktut for in-depth details and examples.

like image 182
Zaid Avatar answered Nov 01 '22 13:11

Zaid


Instead of using split, try the old-school substr method:

my $first = substr($input, 0, 4);
my $second = substr($input, 5, 10);
# etc...

(I like the unpack method too, but substr is easier to write without consulting the documentation, if you're only parsing out a few fields.)

like image 36
Ether Avatar answered Nov 01 '22 12:11

Ether


You could use the substr() function to extract data by offset:

$first = substr($line, 0, 4);
$second = substr($line, 4, 11);

Another option is to use a regular expression:

($first, $second) = ($line =~ /(.{4})(.{11})/);
like image 4
Adam Batkin Avatar answered Nov 01 '22 11:11

Adam Batkin