in reply to One liner: split fixed-width field into equal length chunks...

This anwser disqualifies itself as a one-liner from the start. If you're looking for a good one liner, BrowserUK's last answer is a beauty. However, in the spirit of TIMTOWTDI and 'getting to know your unpack', curiosity piqued me to use unpack to split a string:
use strict; use Data::Dumper; my $string="abcdefghijklmnopqrstuvwxyz"; my $aryref= []; chunk_em($string,$aryref,5); print Dumper($aryref); sub chunk_em { my $string=shift; my $aryref=shift; my $num=shift; my ($first, $second)=unpack("A${num}A*", $string); push @$aryref, $first; chunk_em($second,$aryref,5) if $second; return $aryref; } __END__ $VAR1 = [ 'abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy', 'z' ];
Not a one liner by any stretch of imagination, so this is FYI only.

CU
Robartes-

  • Comment on Re: One liner: split fixed-width field into equal length chunks...
  • Download Code