如何在Perl中创建变量数组名称?

时间:2022-05-29 23:20:33

Array @p is a multiline array, e.g. $p[1] is the second line.

Array @p是一个多行数组,例如$ p [1]是第二行。

This code will explain what I want:

这段代码将解释我想要的东西:

$size=@p;   # line number of array @p
for($i=0; $i<$size; $i++)
{ 
  @p{$i}= split(/ +/,$p[$i]);
}

I want the result should be like this:

我希望结果应该是这样的:

@p0 = $p[0]          first line of array @p goes to array @p0;       
@p1 = $p[1]          second line of array @p goes to array @p1; 
...
...

and so on.

等等。

But above code does not work, how can I do it?

但是上面的代码不起作用,我该怎么办呢?

2 个解决方案

#1


4  

It is a bad idea to dynamically generate variable names.

动态生成变量名称是个坏主意。

I suggest the best solution here is to convert each line in your @p array to an array of fields.

我建议这里最好的解决方案是将@p数组中的每一行转换为字段数组。

Lets suppose you have a better name for @p, say @lines. Then you can write

让我们假设你有一个更好的@p名称,比如说@lines。然后你就可以写了

my @lines = map [ split ], <$fh>;

to read in all the lines from the file handle $fh and split them on whitespace. The first field of the first line is then $lines[0][0]. The third field of the first line is $lines[0][2] etc.

读入文件句柄$ fh中的所有行并将它们拆分为空格。第一行的第一个字段是$ lines [0] [0]。第一行的第三个字段是$ lines [0] [2]等。

#2


3  

First, the syntax @p{$i} accesses the entry with the key $i in a hash %p, and returns it in list context. I don't think you meant that. use strict; use warnings; to get warned about undeclared variables.

首先,语法@p {$ i}在散列%p中使用键$ i访问该条目,并在列表上下文中返回它。我不认为你的意思。用严格;使用警告;警告未申报的变量。

You can declare variables with my, e.g. my @p; or my $size = @p;

您可以使用我的声明变量,例如我的@p;或我的$ size = @p;

Creating variable names on the fly is possible, but a bad practice. The good thing is that we don't need to: Perl has references. A reference to an array allows us to nest arrays, e.g.

可以动态创建变量名称,但这是一种不好的做法。好消息是我们不需要:Perl有参考。对数组的引用允许我们嵌套数组,例如

my @AoA = (
   [1, 2, 3],
   ["a", "b"],
);
say $AoA[0][1]; # 2
say $AoA[1][0]; # a

We can create an array reference by using brackets, e.g. [ @array ], or via the reference operator \:

我们可以使用括号创建数组引用,例如[@array],或通过引用运算符\:

my @inner_array = (1 .. 3);
my @other_inner = ("a", "b");
my @AoA = (\@inner_array, \@other_array);

But careful: the array references still point to the same array as the original names, thus

但要小心:数组引用仍然指向与原始名称相同的数组,因此

push @other_inner, "c";

also updates the entry in @AoA:

还会更新@AoA中的条目:

say $AoA[1][2]; # c

Translated to your problem this means that you want:

转换为您的问题这意味着您想要:

my @pn;
for (@p) { 
  push @pn, [ split /[ ]+/ ];
}

There are many other ways to express this, e.g.

还有许多其他方式来表达这一点,例如:

my @pn = map [ split /[ ]+/ ], @p;

or

要么

my @pn;
for my $i ( 0 .. $#p ) {
  $pn[$i] = [ split /[ ]+/, $p[$i] ];
}

To learn more about references, read

要了解有关参考的更多信息,请阅读

#1


4  

It is a bad idea to dynamically generate variable names.

动态生成变量名称是个坏主意。

I suggest the best solution here is to convert each line in your @p array to an array of fields.

我建议这里最好的解决方案是将@p数组中的每一行转换为字段数组。

Lets suppose you have a better name for @p, say @lines. Then you can write

让我们假设你有一个更好的@p名称,比如说@lines。然后你就可以写了

my @lines = map [ split ], <$fh>;

to read in all the lines from the file handle $fh and split them on whitespace. The first field of the first line is then $lines[0][0]. The third field of the first line is $lines[0][2] etc.

读入文件句柄$ fh中的所有行并将它们拆分为空格。第一行的第一个字段是$ lines [0] [0]。第一行的第三个字段是$ lines [0] [2]等。

#2


3  

First, the syntax @p{$i} accesses the entry with the key $i in a hash %p, and returns it in list context. I don't think you meant that. use strict; use warnings; to get warned about undeclared variables.

首先,语法@p {$ i}在散列%p中使用键$ i访问该条目,并在列表上下文中返回它。我不认为你的意思。用严格;使用警告;警告未申报的变量。

You can declare variables with my, e.g. my @p; or my $size = @p;

您可以使用我的声明变量,例如我的@p;或我的$ size = @p;

Creating variable names on the fly is possible, but a bad practice. The good thing is that we don't need to: Perl has references. A reference to an array allows us to nest arrays, e.g.

可以动态创建变量名称,但这是一种不好的做法。好消息是我们不需要:Perl有参考。对数组的引用允许我们嵌套数组,例如

my @AoA = (
   [1, 2, 3],
   ["a", "b"],
);
say $AoA[0][1]; # 2
say $AoA[1][0]; # a

We can create an array reference by using brackets, e.g. [ @array ], or via the reference operator \:

我们可以使用括号创建数组引用,例如[@array],或通过引用运算符\:

my @inner_array = (1 .. 3);
my @other_inner = ("a", "b");
my @AoA = (\@inner_array, \@other_array);

But careful: the array references still point to the same array as the original names, thus

但要小心:数组引用仍然指向与原始名称相同的数组,因此

push @other_inner, "c";

also updates the entry in @AoA:

还会更新@AoA中的条目:

say $AoA[1][2]; # c

Translated to your problem this means that you want:

转换为您的问题这意味着您想要:

my @pn;
for (@p) { 
  push @pn, [ split /[ ]+/ ];
}

There are many other ways to express this, e.g.

还有许多其他方式来表达这一点,例如:

my @pn = map [ split /[ ]+/ ], @p;

or

要么

my @pn;
for my $i ( 0 .. $#p ) {
  $pn[$i] = [ split /[ ]+/, $p[$i] ];
}

To learn more about references, read

要了解有关参考的更多信息,请阅读