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
Perl-在配置txt文件上使用数组_Perl - Fatal编程技术网

Perl-在配置txt文件上使用数组

Perl-在配置txt文件上使用数组,perl,Perl,因此,我有一个文本文件,一行有四组数据,例如aa bb username password。到目前为止,我已经能够使用子字符串和索引解析文件的第一行,并将这四行中的每一行分配给变量 我的目标是使用数组和chomp遍历每一行并将它们分配给四个变量,然后将用户输入的参数与第一个变量匹配,并在正确的行中使用这四个变量 例如,这将是文本文件: "aa bb cc dd" "ee ff gg hh" 根据用户是输入“aa”还是“ee”作为参数,它将在代码中使用该行的参数集 我正试图建立一个基本数组

因此,我有一个文本文件,一行有四组数据,例如
aa bb username password
。到目前为止,我已经能够使用子字符串和索引解析文件的第一行,并将这四行中的每一行分配给变量

我的目标是使用数组和chomp遍历每一行并将它们分配给四个变量,然后将用户输入的参数与第一个变量匹配,并在正确的行中使用这四个变量

例如,这将是文本文件:

"aa bb cc dd"
"ee ff gg hh"   
根据用户是输入“aa”还是“ee”作为参数,它将在代码中使用该行的参数集

我正试图建立一个基本数组,并根据第一个变量的条件,从本质上说,对它进行检查

这是我为第一行的四个变量编写的代码,但正如我所说,这只适用于文本文件的第一行:

local $/;
open(FILE, $configfile) or die "Can't read config file 'filename' [$!]\n";  
my $document = <FILE>; 
close (FILE);  



my $string = $document;
my $substring = " ";

my $Index = index($string, $substring);
my $aa = substr($string, 0, $Index);
my $newstring = substr($string, $Index+1);

my $Index2 = index($newstring, $substring); 
my $bb = substr($newstring, 0, $Index2);
my $newstring2 = substr($newstring, $Index2+1);

my $Index3 = index($newstring2, $substring);
my $cc = substr($newstring2, 0, $Index3);
my $newstring3 = substr($newstring2, $Index3+1);

my $Index4 = index($newstring3, $substring);
my $dd = substr($newstring3, 0, $Index4);
local$/;
打开(文件,$configfile)或死亡“无法读取配置文件‘文件名’[$!]”\n;
我的$document=;
关闭(文件);
my$string=$document;
我的$substring=“”;
my$Index=索引($string,$substring);
my$aa=substr($string,0,$Index);
my$newstring=substr($string,$Index+1);
my$Index2=索引($newstring,$substring);
my$bb=substr($newstring,0,$Index2);
我的$newstring2=substr($newstring,$Index2+1);
my$Index3=索引($newstring2$substring);
my$cc=substr($newstring2,0,$Index3);
my$newstring3=substr($newstring2$Index3+1);
my$Index4=索引($newstring3$substring);
my$dd=substr($newstring3,0,$Index4);

我的$document=
仅读取第一行。在循环时尝试使用

您正在读取
my$document=
行中的整个文件

尝试以下方法:

my @lines;
open my $file, '<', $configfile or die 'xxx';
while( <$file> ) {
  chomp;
  push @lines, [ split ]
}
my@行;

打开我的$file,“如果您想一次读取文件的所有行(假设它是一个小文件),您可能需要使用
file::Slurp
模块:

use File::Slurp;
my @lines = File::Slurp::read_file($configfile);

foreach my $line (@lines) {
    # do whatever
此外,还可以使用CPAN模块将字符串拆分为字段

如果它们是单空格分隔的,只需使用标准的CSV解析器读取整个文件(您可以将
Text::csvxs
配置为使用任何字符作为分隔符)。示例如下:


如果它们被任意数量的空格分隔,请使用下面@massa的建议并使用
split
函数。

首先,您可以使用而不是在它们上运行索引和子字符串来解析整行内容:

my ( $aa, $bb, $cc, $dd ) = split /\s+/, $line;
更好的方法是使用数组:

my @array = split /\s+/, $line;
my @array;
$array[0] = $reference;
我想您是说需要将每个命令部分数组存储到另一个行数组中。对吗?请看一下Perl文档中提供的这一点

Perl有三种不同类型的变量。问题是,这些变量的每种类型只存储一段数据。数组和散列可以存储大量数据,但在散列或数组的每个元素中只能存储一段数据

引用允许您绕过此限制。引用只是指向另一段数据的指针。例如,如果
$line
=
aa bb cc dd
,则执行以下操作:

 my @command_list = split /\s+/ $line;
将为您提供以下信息:

$command_list[0] = "aa";
$command_list[1] = "bb";
$command_list[2] = "cc";
$command_list[3] = "dd";
您想将
@command\u list
存储到另一个结构中。您需要的是对
@command\u list
的引用。要获得对它的引用,只需在它前面加一个反斜杠:

my $reference = \@command_list;
my @new_array = @{ $reference };
这可以放入一个数组中:

my @array = split /\s+/, $line;
my @array;
$array[0] = $reference;
现在,我将整个数组存储到数组的单个元素中

要从引用返回到原始结构,请放置正确的。因为这是一个数组,所以将
@
放在它前面:

my $reference = \@command_list;
my @new_array = @{ $reference };
如果您希望引用中的第一项不必将其传输到另一个数组中,则可以将
@{$reference}
视为数组本身:

${ $reference }[0] = "aa";
或者,使用神奇的
->
,使语法更清晰:

$reference->[0] = "aa";
完成教程。这将帮助您了解引用的全部功能,以及如何使用它们。您的程序将如下所示:

use strict;
use warnings;
use feature qw(say);  #Better print that print
use autodie;          #Kills your program if the file can't be open

my $file = [...]   #Somehow get the file you're reading in...
open my $file_fh, "<", $file;
my @command_list;
while ( my $line = <$file_fh> ) {
    chomp $line;
    my @line_list = split /\s+/, $line;
    push @command_list, \@line_list;
}

阅读,了解和。

您甚至可以执行
my@lines=map[split],阅读\u文件$configfile
:-d感谢您提供有关
local$/的提醒行。我想我的主要问题是关于数组本身,我如何将数组的四个部分设置为四个不同的变量?因此,我可以对照变量1检查用户输入,以选择在代码中使用哪一个4数组在我发布代码后,
@lines
已经有一个数组了。。。因此,第一行中的第一部分在
$lines[0][0]
中,第二部分在
$lines[0][1]
中,第三部分在
$lines[0][1]
中,第四部分在
$lines[0][3]
中。第二行在
$lines[1][…]
等中。