如何创建的目录内容Perl中的数组? [英] How can I create an array of directory contents in Perl?

查看:111
本文介绍了如何创建的目录内容Perl中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 $目录的内容,使3阵列。
第一个我可以使用:

I need to make 3 arrays from the contents of $directory. The first one I can use:

$var = qx{ls $directory};
print $var;

然而,第二和第三欲对grep主*和二次*

However the second and third I want to grep for "Primary *" and "Secondary *"

我用的命令:

my @primary = 0;
my @secondary = 0;

@primary = system("ls test_77 > test | grep Primary* test");
print @primary;
@secondary = system("ls test_77 > test | grep Secondary* test");
print @secondary;

但,它包括在输出一个零:

BUT, it includes a zero on the output:

PrimaryCPE_Option_A.txt.test_77
PrimaryCPE_Option_B.txt.test_77
0SecondaryCPE_Option_A.txt.test_77
SecondaryCPE_Option_B.txt.test_77
0

这里是什么地方?起初看起来它可能是原始零,但我可以改变的任一初始值和它不工作

What can it be? At first it looks like it might be the original zero, but I can change the initial value of either and it doesn't work.

推荐答案

Perl可以做到这一点,而无需调用系统命令。

Perl can do this without calling system commands.

@secondary=glob("*Secondary*.txt");
print @secondary;
@primary=glob("*Primary*.txt")
print @primary;

使用执行opendir()/ readdir的()的其他方式,或while循环如

Other ways are using opendir()/readdir(), or while loop eg

while(my $file=<*Primary*txt>){
  push(@secondary,$file);
}

或者

push (@secondary,$_) while<*Secondary*.txt> ;

这篇关于如何创建的目录内容Perl中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆