Note also, as an alternative to substr, that pack with the A template will either truncate or pad to the right with spaces.
johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MData::Dumper -e + ' open my $inFH, q{<}, \ <<__EOD__ or die $!; abcdef abcdefg abcde bcdefgh bcd __EOD__ chomp( my @lines = <$inFH> ); close $inFH or die $!; my $width = 6; my $height = 4; my $raRect = makeRect( \ @lines, $width, $height ); print Data::Dumper->Dumpxs( [ $raRect ], [ qw{ raRect } ] ); sub makeRect { my( $raLines, $width, $height ) = @_; my @rect; push @rect, pack qq{A$width}, shift @{ $raLines } for 1 .. $height +; return \ @rect; }' $raRect = [ 'abcdef', 'abcdef', 'abcde ', ' bcdef' ];
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: rectangularizing input to become array
by johngg
in thread rectangularizing input to become array
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |