Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数字从文件读入变量(Perl)_Perl_Variables_Numbers - Fatal编程技术网

将数字从文件读入变量(Perl)

将数字从文件读入变量(Perl),perl,variables,numbers,Perl,Variables,Numbers,我一直在尝试编写一个程序,将文本格式的数字列读入Perl变量 基本上,我有一个包含描述和编号的文件: ref 5.25676 0.526231 6.325135 ref 1.76234 12.62341 9.1612345 等等 我想把这些数字放进不同名称的变量中,例如 ref_1_x=5.25676 ref_1_y=0.526231 等等 以下是到目前为止我得到的信息: print "Loading file ..."; open (FILE

我一直在尝试编写一个程序,将文本格式的数字列读入Perl变量

基本上,我有一个包含描述和编号的文件:

ref   5.25676      0.526231      6.325135
ref   1.76234     12.62341       9.1612345
等等

我想把这些数字放进不同名称的变量中,例如

ref_1_x=5.25676
ref_1_y=0.526231
等等

以下是到目前为止我得到的信息:

print "Loading file ...";
open (FILE, "somefile.txt");
@text=<FILE>;
close FILE;
print "Done!\n";
my $count=0;
foreach $line (@text){
    @coord[$count]=split(/ +/, $line);
}
打印“加载文件…”;
打开(文件“somefile.txt”);
@文本=;
关闭文件;
打印“完成!\n”;
我的$count=0;
foreach$行(@text){
@坐标[$count]=拆分(/+/,$line);
}

我正在尝试比较文件中写入的位置,因此在此之后需要另一个循环。

抱歉,您不太清楚您要做什么以及“ref”指的是什么。如果我误解了你的问题,请推荐并澄清


首先,我强烈建议不要使用变量名称来构造数据(例如,使用
$ref\u 1\u x
来存储标签为“ref”的第一行的x坐标)

如果要存储x、y和z坐标,可以将其作为一个由3个元素组成的数组来存储,这与您之前所做的非常相似-唯一的区别是您要存储一个数组引用(在Perl中不能将数组作为值存储在另一个数组中):

然后,要访问第2行的x坐标,请执行以下操作:

$coordinates[1]->[0]; # index 1 for row 2; then sub-index 0 for x coordinate.
$coordinates[1]->{x}; # index 1 for row 2

如果您坚持将3个坐标存储在命名数据结构中,因为x坐标的子索引0看起来不太可读-这通常是一个有效的问题,但实际上不是3列的问题-请使用哈希而不是数组:

my ($first_column, @data) = split(/ +/, $line); # Remove first "ref" column
@coordinates[$count++] = { x => $data[0], y => $data[1], z => $data[2] };
# curly braces - {} - to store hash reference again
然后,要访问第2行的x坐标,请执行以下操作:

$coordinates[1]->[0]; # index 1 for row 2; then sub-index 0 for x coordinate.
$coordinates[1]->{x}; # index 1 for row 2

现在,如果您还想将第一列值为“ref”的行存储在单独的带有“ref”标签的数据结构中,您可以通过将原始
@coordinates
数组包装成散列中的值,并使用键“ref”来实现

然后,要访问标签“ref”第2行的x坐标,需要执行以下操作:

另外,您的原始示例代码中有一些过时的习惯用法,您可能希望以更好的风格编写(使用
open()
的三参数形式,检查诸如
open()
之类的IO操作的错误;使用词法文件句柄;将整个文件存储在一个大数组中,而不是逐行读取)

这里有一个稍微修改过的版本:

use strict;
my %coordinates;
print "Loading file ...";
open (my $file, "<", "somefile.txt") || die "Can't read file somefile.txt: $!";
while (<$file>) {
    chomp;
    my ($label, @data) = split(/ +/); # Splitting $_ where while puts next line
    $coordinates{$label} ||= []; # Assign empty array ref if not yet assigned
    push @{ $coordinates{$label} }
       , { x => $data[0], y => $data[1], z => $data[2] };
}
close($file);
print "Done!\n";
使用严格;
我的%坐标;
打印“正在加载文件…”;

打开(我的$file,“对不起,您不太清楚您要做什么以及“ref”指的是什么。如果我误解了您的问题,请推荐并澄清


首先,我强烈建议不要使用变量名称来构造数据(例如,使用
$ref\u 1\u x
来存储标签为“ref”的第一行的x坐标)

如果要存储x、y和z坐标,可以将其作为一个由3个元素组成的数组来存储,这与您之前所做的非常相似-唯一的区别是您要存储一个数组引用(在Perl中不能将数组作为值存储在另一个数组中):

然后,要访问第2行的x坐标,请执行以下操作:

$coordinates[1]->[0]; # index 1 for row 2; then sub-index 0 for x coordinate.
$coordinates[1]->{x}; # index 1 for row 2

如果您坚持将3个坐标存储在命名数据结构中,因为x坐标的子索引0看起来不太可读-这通常是一个有效的问题,但实际上不是3列的问题-请使用哈希而不是数组:

my ($first_column, @data) = split(/ +/, $line); # Remove first "ref" column
@coordinates[$count++] = { x => $data[0], y => $data[1], z => $data[2] };
# curly braces - {} - to store hash reference again
然后,要访问第2行的x坐标,请执行以下操作:

$coordinates[1]->[0]; # index 1 for row 2; then sub-index 0 for x coordinate.
$coordinates[1]->{x}; # index 1 for row 2

现在,如果您还想将第一列值为“ref”的行存储在单独的带有“ref”标签的数据结构中,您可以通过将原始
@coordinates
数组包装成散列中的值,并使用键“ref”来实现

然后,要访问标签“ref”第2行的x坐标,需要执行以下操作:

另外,您的原始示例代码中有一些过时的习惯用法,您可能希望以更好的风格编写(使用
open()
的三参数形式,检查诸如
open()
之类的IO操作的错误;使用词法文件句柄;将整个文件存储在一个大数组中,而不是逐行读取)

这里有一个稍微修改过的版本:

use strict;
my %coordinates;
print "Loading file ...";
open (my $file, "<", "somefile.txt") || die "Can't read file somefile.txt: $!";
while (<$file>) {
    chomp;
    my ($label, @data) = split(/ +/); # Splitting $_ where while puts next line
    $coordinates{$label} ||= []; # Assign empty array ref if not yet assigned
    push @{ $coordinates{$label} }
       , { x => $data[0], y => $data[1], z => $data[2] };
}
close($file);
print "Done!\n";
使用严格;
我的%坐标;
打印“正在加载文件…”;

打开(my$file,“问题是您可能需要一个双数组(或哈希或…)。而不是这样:

@coord[$count]=split(/ +/, $line);
使用:

将整个分割结果放入子数组中。因此

print $coord[0][1];

应该输出“5.25676”。

问题是您可能需要一个双数组(或哈希或…)。而不是这个:

@coord[$count]=split(/ +/, $line);
使用:

将整个分割结果放入子数组中。因此

print $coord[0][1];

应该输出“5.25676”。

那么您想比较文件中不同行的数字吗?那么您想比较文件中不同行的数字吗?这有一个“bug”(功能?),它存储数组中的第一个“ref”列以及坐标它是一个功能;-)如果您真的不想要,可以使用连接到拆分的拼接轻松地删除它。拼接(split(),0,1);这有一个“bug”(功能?),它存储数组中的第一个“ref”列以及坐标它是一个功能;-)如果您真的不想要,可以使用连接到拆分的拼接轻松地删除它。拼接(拆分(),0,1);