如何以矩阵格式打印数组
How to print an array in a matrix format
我正在创建一个密码程序。我想将密钥(在 $ARGV[1]
中给出)转录为数字矩阵。
但是,我在弄清楚如何在不获取 warnings/errors 的情况下将数组打印为矩阵时遇到一些麻烦。
use strict;
use warnings;
use POSIX;
my @characters = split //, $ARGV[1];
@characters = map {ord($_)} 0 .. $#characters;
my $col_nb = ceil(sqrt($#characters));
for my $i (1 .. ($col_nb**2 - $#characters - 1)) { push @characters , 0; }
foreach my $i (0 .. $col_nb - 1) {
printf "%.0f\t" x $col_nb, @characters[$col_nb * $i .. $col_nb * ($i + 1)];
printf("\n");
}
我正在努力获得这样的输出:(key = "abcd")
48 49
50 51
但是,我在输出中得到了这些错误:
Redundant argument in printf at test.perl line 9.
48 49
Redundant argument in printf at test.perl line 9.
50 51
你差一分。您的数组切片包含 3 个数字,但您只需要 2 个。更改:
printf "%.0f\t" x $col_nb, @characters[$col_nb * $i .. $col_nb * ($i + 1)];
至:
printf "%.0f\t" x $col_nb, @characters[$col_nb * $i .. ($col_nb * ($i + 1) - 1)];
您可以添加 use diagnostics;
以获得更详细的警告消息:
(W redundant) You called a function with more arguments than other
arguments you supplied indicated would be needed. Currently only
emitted when a printf-type format required fewer arguments than were
supplied, but might be used in the future for e.g. "pack" in perlfunc.
仅在格式化时使用 printf
。变化:
printf("\n");
至:
print "\n";
检查边缘情况和差一错误。
此外,您需要字符数的平方根,而不是数字 - 1。
此外,您不需要创建另一个数组来保存数字,您可以在打印时将字符动态映射到它们。
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw{ ceil };
my @characters = split //, $ARGV[1];
my $col_nb = ceil(sqrt @characters);
for my $i (0 .. $col_nb - 1) {
printf "%d\t" x $col_nb,
map defined ? ord : 0,
@characters[$col_nb * $i .. $col_nb * ($i + 1) - 1];
print "\n";
}
我正在创建一个密码程序。我想将密钥(在 $ARGV[1]
中给出)转录为数字矩阵。
但是,我在弄清楚如何在不获取 warnings/errors 的情况下将数组打印为矩阵时遇到一些麻烦。
use strict;
use warnings;
use POSIX;
my @characters = split //, $ARGV[1];
@characters = map {ord($_)} 0 .. $#characters;
my $col_nb = ceil(sqrt($#characters));
for my $i (1 .. ($col_nb**2 - $#characters - 1)) { push @characters , 0; }
foreach my $i (0 .. $col_nb - 1) {
printf "%.0f\t" x $col_nb, @characters[$col_nb * $i .. $col_nb * ($i + 1)];
printf("\n");
}
我正在努力获得这样的输出:(key = "abcd")
48 49
50 51
但是,我在输出中得到了这些错误:
Redundant argument in printf at test.perl line 9.
48 49
Redundant argument in printf at test.perl line 9.
50 51
你差一分。您的数组切片包含 3 个数字,但您只需要 2 个。更改:
printf "%.0f\t" x $col_nb, @characters[$col_nb * $i .. $col_nb * ($i + 1)];
至:
printf "%.0f\t" x $col_nb, @characters[$col_nb * $i .. ($col_nb * ($i + 1) - 1)];
您可以添加 use diagnostics;
以获得更详细的警告消息:
(W redundant) You called a function with more arguments than other
arguments you supplied indicated would be needed. Currently only
emitted when a printf-type format required fewer arguments than were
supplied, but might be used in the future for e.g. "pack" in perlfunc.
仅在格式化时使用 printf
。变化:
printf("\n");
至:
print "\n";
检查边缘情况和差一错误。
此外,您需要字符数的平方根,而不是数字 - 1。
此外,您不需要创建另一个数组来保存数字,您可以在打印时将字符动态映射到它们。
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw{ ceil };
my @characters = split //, $ARGV[1];
my $col_nb = ceil(sqrt @characters);
for my $i (0 .. $col_nb - 1) {
printf "%d\t" x $col_nb,
map defined ? ord : 0,
@characters[$col_nb * $i .. $col_nb * ($i + 1) - 1];
print "\n";
}