Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
Regex Perl-输出不一致_Regex_Perl - Fatal编程技术网

Regex Perl-输出不一致

Regex Perl-输出不一致,regex,perl,Regex,Perl,这是匹配用户及其家庭存储容量的代码,奇怪的是,它会产生五次相同的结果,而不是预期的五次唯一结果 open( my $storage_input, "du -shc /home/*|" ); while ( my $line3 = <$storage_input> ) { push( @user_storage, $line3 ); } foreach my $line3 ( @user_storage ) { if ( $line3 =~ m/^([0-9](.)

这是匹配用户及其家庭存储容量的代码,奇怪的是,它会产生五次相同的结果,而不是预期的五次唯一结果

open( my $storage_input, "du -shc /home/*|" );

while ( my $line3 = <$storage_input> ) {
    push( @user_storage, $line3 );
}

foreach my $line3 ( @user_storage ) {
    if ( $line3 =~ m/^([0-9](.){1,})M\s+\/[a-z]{1,4}\/([a-z]{4}|[a-z]{1}\d{2}[a-z]{5})/ ) {
        %storage = ( "data" => "$1", "user" => "$3", );
        push( @storage_data, \%storage );
    }

}

print "\n";

foreach ( my $i = 0 ; $i < scalar( @storage_data ) ; $i++ ) {
    print $storage_data[$i]{"user"};
    print " has used ";
    print $storage_data[$i]{"data"};
    print " MiB of storage.\n";
}

将对
%storage
的引用推送到
@storage\u data
,然后更改
%storage
。对于循环的每次迭代,您应该使用不同的
%存储
,即将
my
前置到其赋值

my %storage = (
您是否使用
严格的

或者,您可以存储匿名哈希:

push @storage_data, { data => "$1",
                      user => "$3",
                    };

du-shc/home/*
的输出是什么?代码的输出是什么?您每次都在推送相同的引用。请尝试本地化新的storate实例(my%storate=)或只执行任何href
push(@storage\u data,{“data”=>“$1”,“user”=>“$3})谢谢。它成功了。围绕
$1
$3
的多余引号?虽然我想你有你的理由@Borodin:我更喜欢每次引用
$1
变量,即使上下文是无害的(如下所示)。但是,当使用无引号的函数参数时,它们会做一些奇怪的事情。@Borodin:e.g。。。
push @storage_data, { data => "$1",
                      user => "$3",
                    };