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

#!/usr/bin/perl -w use strict; sub fixedsplit { my ($data,$len) = @_; ($data =~ /(.{1,$len})/g); } my $data = "abcdefghijklmnopqrstuvwxyz"; my @fields; @fields = fixedsplit($data,4); print "@fields\n"; @fields = fixedsplit($data,6); print "@fields\n";

Produces:

abcd efgh ijkl mnop qrst uvwx yz abcdef ghijkl mnopqr stuvwx yz
--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Replies are listed 'Best First'.
Re: Re: One liner: split fixed-width field into equal length chunks...
by mr. jaggers (Sexton) on Mar 25, 2003 at 04:42 UTC
    Yes, this is clearer, and is actually pretty close to what I settled on (before the latter Monks replied). Thanks, pfaut!