sách gpt4 ai đã đi

perl - 将二维数组从一个 Perl 脚本传递到另一个

In lại 作者:行者123 更新时间:2023-12-04 02:53:52 28 4
mua khóa gpt4 Nike

我有一个名为 master.pl 的 Perl 脚本。我有一个名为 @inputarray 的二维数组。

我需要将二维数组值从 master.pl 传递到另一个名为 child.pl 的程序并访问 child.pl .

我已经尝试了很多,但我无法取消引用 child.pl 中的数组。

你能帮帮我吗?

master.pl

system "start perl child.pl $appl $count @inputarray";

child.pl

($appl, $count, @inputarray) = @ARGV;

for (my $k = 0; $k < $count + 1; $k++) {
for (my $m = 0; $m < 6; $m++) {
print "$inputarray[$k][$m] ";
}
print "\n";
}

1 Câu trả lời

方法一:

看看标准模块Data::Dumper , 它是您想要的理想选择。

使用 Data::Dumper 将您的数据结构保存在一个临时文件中,然后在您的第二个脚本中读取它。

方法二:

sử dụng Storable在第一个脚本中存储数组并从其他脚本中检索它。

编辑(在您提供代码之后):

看到你可以像这样访问数组

master.pl

#!/usr/local/bin/perl
sử dụng nghiêm ngặt;
use warnings;
use Storable;
my @inputarray = ([1, 2, 3], [4, 5, 6], [7, 8, 9]);
store (\@inputarray, "/home/chankey/child.$$") or die "could not store";
system("perl", "child.pl", $$) == 0 or die "error";

child.pl

#/usr/local/bin/perl
sử dụng nghiêm ngặt;
use warnings;
use Storable;
use Data::Dumper;
my $parentpid = shift;
my $ref = retrieve("/home/chankey/child.$parentpid") or die "coudn't retrieve";
print Dumper $ref;
print $$ref[0][0]; #prints 1

Đầu ra

$VAR1 = [
[
1,
2,
3
],
[
4,
5,
6
],
[
7,
8,
9
]
]; #from Dumper
1 #from print $$ref[0][0]

正如您从转储中看到的那样,您已经在$ref 中收到了@inputarray。现在按照您想要的方式使用它。

关于perl - 将二维数组从一个 Perl 脚本传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082007/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com