I'm attempting to present an SSCCE.
Note that an SSCCE is all about making things clear and easy for the people trying to help: no extra editing of the inputs, no guessing which input produces which output, guessing whether commented-out code is relevant to the question, and so on. For me, an ideal question would contain three of PerlMonks' [download] links that I can right-click and do "Save As" on: the input file, the output file, and the source code, such that I can just download them and run perl script.pl input.txt | diff - output.txt and see what's going on. There are also a couple other options, such as embedding the input in the source in the form of a __DATA__ section or just a variable, and embedding the output in the source and using e.g. Test::More to check if it matches.
In your question, you've included a link to GitHub, which might go down sometime in the future (plus it's a couple more clicks), and also, in your <code> tags you've included the command-line invocations that I'd have to trim, and that aren't really necessary here to show others how to run the script or what's going on. Also, since the question is about whitespace, the dd invocation you commented out is actually more useful than a plain print here.
I read somewhere that a good name for a default directory in perl development is 'lib'.
Yes, but mostly for .pm files - the lib directory is usually what would get added to @INC such that use and require (and in some cases do) can find the files. Typically, the content of such directories is not what would get modified by a user or get modified during the run of a script.
specified by $width and $length.
Personally, I find those variable names a little confusing: You've got an array of strings, and $length sounds like it refers to the strings' length, but it seems like that's what $width is for. I might suggest $width/$height, $length/$height, $length/$rows, or $cols/$rows.
What I would like is for these inputs to get trimmed to an array of the size specified by $width and $length . Spaces added/substituted on right if necessary to pad each vector to the same size.
You could use substr for the trimming and sprintf for the output with padding.
use warnings; use strict; use Data::Dump; my $input = <<'(END INPUT)'; abcdef abcdefg abcde bcdefgh bcd (END INPUT) my @lines = split /\n/, $input; dd \@lines; my $out = make_rectangular( \@lines, 4, 6 ); dd $out; sub make_rectangular { my ( $lines, $maxrows, $maxlength ) = @_; my @out; my $rowcount=1; for my $line (@$lines) { my $trimmed = substr $line, 0, $maxlength; push @out, sprintf "%-*s", $maxlength, $trimmed; last if ++$rowcount>$maxrows; } return \@out; } __END__ ["abcdef", "abcdefg", "abcde", " bcdefgh", " bcd\t"] ["abcdef", "abcdef", "abcde ", " bcdef"]
(Note I've assumed you want to not modify the input array here.) Of course TIMTOWTDI, I could've mashed the code into a single map statement, but I hope this is a little more clear.
BTW, I'm not sure what you want to do with the tab character in your input file? This code counts it as a single character.
Also, what character is it that renders an entire space dark?
Do you mean the block drawing character U+2588, "█"? That would be "\N{U+2588}" or "\N{FULL BLOCK}". I might suggest U+2420, "␠" ("\N{U+2420}" or "\N{SYMBOL FOR SPACE}", see also). Note that for the \N{CHARNAME} variant, you may have to add use charnames ':full'; to your script, depending on your Perl version (newer versions load it automatically).
In reply to Re: rectangularizing input to become array
by haukex
in thread rectangularizing input to become array
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |