Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.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 - Fatal编程技术网

如何使用perl从文件中读取特定行并存储在数组中?

如何使用perl从文件中读取特定行并存储在数组中?,perl,Perl,如何将文件中未注释的行读取/存储到数组中 file.txt如下所示 request abcd uniquename "zxsder,azxdfgt" request abcd uniquename1 "nbgfdcbv.bbhgfrtyujk" request abcd uniquename2 "nbcvdferr,nscdfertrgr" #request abcd uniquename3 "kdgetgsvs,jdgdvnhur" #request abcd uniquename4 "hvg

如何将文件中未注释的行读取/存储到数组中

file.txt
如下所示

request abcd uniquename "zxsder,azxdfgt"
request abcd uniquename1 "nbgfdcbv.bbhgfrtyujk"
request abcd uniquename2 "nbcvdferr,nscdfertrgr"
#request abcd uniquename3 "kdgetgsvs,jdgdvnhur"
#request abcd uniquename4 "hvgsfeyeuee,bccafaderryrun"
#request abcd uniquename5 "bccsfeueiew,bdvdfacxsfeyeueiei"
现在,我必须将未注释的行(本脚本中的前3行)读取/存储到一个数组中。是否可以通过模式匹配字符串名或任何正则表达式来使用它?如果是,我该怎么做

下面的代码将所有行存储到一个数组中

open (F, "test.txt") || die "Could not open test.txt: $!\n"; 
@test = <F>; 
close F; 
print @test;
open(F,“test.txt”)| | die“无法打开test.txt:$!\n”;
@测试=;
关闭F;
打印@测试;

如何仅对未注释的行执行此操作?

如果您知道您的注释将包含#在开始时您可以使用

next if $_ =~ m/^#/
或者使用您必须读取每一行的任何变量,而不是
$\uu

#!/usr/bin/perl

# Should always include these
use strict;
use warnings;

my @lines; # Hold the lines you want

open (my $file, '<', 'test.txt') or die $!; # Open the file for reading
while (my $line = <$file>)
{
  next if $line =~ m/^#/; # Look at each line and if if isn't a comment
  push (@lines, $line);   # we will add it to the array.
}
close $file;

foreach (@lines) # Print the values that we got
{
  print "$_\n";
}
这与行开头的#符号匹配。 至于将其他元素添加到数组中,您可以使用
push(@arr,$\ux)

#/usr/bin/perl
#应始终包括这些
严格使用;
使用警告;
我的@lines;#别挂断

打开(我的$file,如果你知道你的评论将包含#开头你可以使用

next if $_ =~ m/^#/
或者使用您必须读取每一行的任何变量,而不是
$\uu

#!/usr/bin/perl

# Should always include these
use strict;
use warnings;

my @lines; # Hold the lines you want

open (my $file, '<', 'test.txt') or die $!; # Open the file for reading
while (my $line = <$file>)
{
  next if $line =~ m/^#/; # Look at each line and if if isn't a comment
  push (@lines, $line);   # we will add it to the array.
}
close $file;

foreach (@lines) # Print the values that we got
{
  print "$_\n";
}
这与行开头的#符号匹配。 至于将其他元素添加到数组中,您可以使用
push(@arr,$\ux)

!/usr/bin/perl
#应始终包括这些
严格使用;
使用警告;
我的@lines;#别挂线
打开(我的$file),您可以执行以下操作:

push @ary,$_ unless /^#/;END{print join "\n",@ary}'
这将跳过以
#
开头的任何行。否则,该行将添加到数组中供以后使用。

您可以执行以下操作:

push @ary,$_ unless /^#/;END{print join "\n",@ary}'

这将跳过以
#
开头的任何行。否则,该行将添加到数组中供以后使用。

对原始程序的最小更改可能是:

open (F, "test.txt") || die "Could not open test.txt: $!\n"; 
@test = grep { $_ !~ /^#/ } <F>; 
close F; 
print @test;
open(F,“test.txt”)| | die“无法打开test.txt:$!\n”;
@test=grep{$\!~/^};
关闭F;
打印@测试;
但我强烈建议使用当前的最佳实践稍微重写一下

# Safety net
use strict;
use warnings;
# Lexical filehandle, three-arg open
open (my $fh, '<', 'test.txt') || die "Could not open test.txt: $!\n"; 
# Declare @test.
# Don't explicitly close filehandle (closed automatically as $fh goes out of scope)
my @test = grep { $_ !~ /^#/ } <$fh>; 
print @test;
#安全网
严格使用;
使用警告;
#词法文件句柄,三个参数打开

打开(my$fh),对原始程序的最小更改可能是:

open (F, "test.txt") || die "Could not open test.txt: $!\n"; 
@test = grep { $_ !~ /^#/ } <F>; 
close F; 
print @test;
open(F,“test.txt”)| | die“无法打开test.txt:$!\n”;
@test=grep{$\!~/^};
关闭F;
打印@测试;
但我强烈建议使用当前的最佳实践稍微重写一下

# Safety net
use strict;
use warnings;
# Lexical filehandle, three-arg open
open (my $fh, '<', 'test.txt') || die "Could not open test.txt: $!\n"; 
# Declare @test.
# Don't explicitly close filehandle (closed automatically as $fh goes out of scope)
my @test = grep { $_ !~ /^#/ } <$fh>; 
print @test;
#安全网
严格使用;
使用警告;
#词法文件句柄,三个参数打开

open(my$fh,@squiguy:如果此代码读取了文件的所有行,我如何读取未注释的行?open(F,“test.txt”)| | die“无法打开test.txt:$!\n”;@test=;close F;print@test;我将更新我的答案,以了解如何为整个脚本执行您需要的操作。@squiguy:如果此代码读取了文件的所有行,我如何读取未注释的行?open(F,“test.txt”)| die“无法打开test.txt:$!\n;@test=;close F;print@test;我将更新我的答案,以了解如何对整个脚本执行您需要的操作。