英文:
getting a 2d array in perl from input
问题
以下是您要翻译的代码部分:
print("enter number of rows: ");
chomp(my $row = <>);
print("enter number of columns: ");
chomp(my $column = <>);
my @matrix = ();
for (my $r=0; $r<$row; $r=$r+1){
my @list = ();
for(my $c=0; $c<$column; $c=$c+1){
print("input:\n");
chomp(my $input = <>);
push(@list, $input);
}
print("list now @list\n");
push(@matrix, @list);
}
print("final matrix @matrix\n");
print("$matrix[0][0]\n");
请注意,我已将您提供的代码中的 HTML 实体(如 <
和 "
)替换为普通字符,并进行了一些格式调整,以使代码更易于理解。
如果您有任何关于代码的具体问题,或者需要进一步的解释,请随时提出。
英文:
I want to get a 2d array from input in Perl.
I will get 2 inputs for number of rows and column of the matrix and get the input in 2 for loops
First I get two variables named $row and $column from input.
Now what I used to do in python was I made 2 for loops and input like this:
matrix = []
for i in range (row):
temp = []
for j in range (column):
x <- input()
temp.append(x)
matrix.append(temp)
And now in matrix I have the matrix user has inputted.
In Perl though I can't do the same thing.
this is my code:
print("enter number of rows: ");
chomp(my $row = <>);
print("enter number of columns: ");
chomp(my $column = <>);
my @matrix = ();
for (my $r=0; $r<$row; $r=$r+1){
my @list = [];
for(my $c=0; $c<$column; $c=$c+1){
print("input:\n");
chomp(my $input = <>);
push(@list, $input);
}
print("list now @list\n");
push(@matrix, @list);
}
print("final matrix @matrix\n");
print("$matrix[0][0]\n");
This is the output I get after the line print("final matrix @matrix\n"); executes:
final matrix ARRAY(0xddb038) 1 2 ARRAY(0xddb068) 3 4
and after the last print i get this:
Use of uninitialized value in concatenation (.) or string at test.pl line 94, <> line 6.
(btw line 6 is a comment)
this is the whole input and output:
enter number of rows: 2
enter number of columns: 2
input:
1
input:
2
list now ARRAY(0xddb038) 1 2
input:
3
input:
4
list now ARRAY(0xddb068) 3 4
final matrix ARRAY(0xddb038) 1 2 ARRAY(0xddb068) 3 4
Use of uninitialized value in concatenation (.) or string at test.pl line 94, <> line 6.
So basically I want the user to input 2 numbers (n and m) and then input an n*m matrix. I can get the 2 numbers. but when getting the numbers of the matrix, I am doing something wrong in pushing it (appending it) to the main matrix and I think I have not understood the difference of '$' and '@' and also [] and ().
答案1
得分: 3
你不需要push
。你可以将值分配给矩阵的相应行和列($matrix[$i][$j] = $val;
)。
请看下面的脚本:
#!/usr/bin/perl
use strict; use warnings;
print "输入行数:";
my $rows = <STDIN>;
chomp $rows;
print "输入列数:";
my $cols = <STDIN>;
chomp $cols;
my @matrix;
for (my $i=0; $i< $rows; $i++){
for (my $j = 0; $j < $cols; $j++){
print "输入第 $i 行第 $j 列的值:";
chomp (my $val = <STDIN>);
$matrix[$i][$j] = $val;
}
}
print "矩阵:\n";
for (my $i = 0; $i < $rows; $i++){
for (my $j = 0; $j < $cols; $j++){
print "$matrix[$i][$j]\t";
}
print "\n";
}
结果:
输入行数:2
输入列数:2
输入第 0 行第 0 列的值:4
输入第 0 行第 1 列的值:3
输入第 1 行第 0 列的值:2
输入第 1 行第 1 列的值:1
矩阵:
4 3
2 1
英文:
You don't need to push
. You can assign the value to the matrix's respective row and column ($matrix[$i][$j] = $val;
).
See the below script:
#!/usr/bin/perl 
use strict; use warnings;
print "Enter the number of rows:";
my $rows = <STDIN>;
chomp $rows;
print "Enter the number of columns:";
my $cols = <STDIN>;
chomp $cols;
my @matrix;
for (my $i=0; $i< $rows; $i++){
for (my $j = 0; $j < $cols; $j++){
print "Enter value for row $i and column $j:";
chomp (my $val = <STDIN>);
$matrix[$i][$j] = $val;
}
}
print "Matrix:\n";
for (my $i = 0; $i < $rows; $i++){
for (my $j = 0; $j < $cols; $j++){
print "$matrix[$i][$j]\t";
}
print "\n";
}
Result:
Enter the number of rows:2
Enter the number of columns:2
Enter value for row 0 and column 0:4
Enter value for row 0 and column 1:3
Enter value for row 1 and column 0:2
Enter value for row 1 and column 1:1
Matrix:
4 3
2 1
答案2
得分: 3
您混合了两种不同的方法。
使用数组:
my @list; # 创建一个数组。
push( @list, $input ); # 向数组添加元素。
push( @matrix, \@list ); # 向@matrix添加一个对数组的引用。
使用数组的引用:
my $list = []; # 创建一个匿名数组
push( @$list, $input ); # 向数组添加元素。
push( @matrix, $list ); # 向@matrix添加一个对数组的引用。
注意,[]
返回对它创建的匿名数组的引用,因此我们将其存储在标量中。这还意味着我们需要解引用该标量以访问数组(如第二个片段中的中间 push
中所见)。
请注意,数组不能包含数组,因此我们使用数组引用的数组来近似数组的数组(就像Python一样)。因此,如果我们有一个命名的数组,我们需要创建一个引用(例如使用\
)。这是与Python不同的部分,因为在Python中始终存在引用。
我更喜欢第一种方法,但两种方法都可以。
提示:
数组已经是空的。
my @matrix = ();
最好写成
my @matrix;
提示:
C风格的for循环非常紧凑,难以阅读。
for (my $r=0; $r<$row; $r=$r+1)
最好写成
for my $r ( 0 .. $r-1 )
英文:
You're mixing two different approaches.
Working with an array:
my @list; # Create an array.
push( @list, $input ); # Add to it.
push( @matrix, \@list ); # Add a reference to it to @matrix.
Working with a reference to an array:
my $list = []; # Create an anonymous array
push( @$list, $input ); # Add to it.
push( @matrix, $list ); # Add a reference to it to @matrix.
Note that []
returns a reference to the anonymous array it creates, so we store that in a scalar. It also means we need to dereference that scalar to access the array (as see in the middle push
of the second snippet).
Note that arrays can't contain arrays, so we approximate arrays of arrays with arrays of array references. (Like Python, really.) So we need to create a reference (e.g. using \
) if we have a named array. (This is the part that's different than Python, since you always have a reference there.)
I prefer the first approach, but either is fine.
Tip:
Arrays already start empty.
my @matrix = ();
is better written as
my @matrix;
Tip:
C-style for loops are extremely dense, hard to read.
for (my $r=0; $r<$row; $r=$r+1)
is better written as
for my $r ( 0 .. $r-1 )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论