如何在perl中从多维数组中提取列数据

如何在perl中从多维数组中提取列数据,perl,Perl,ls-l 我只需要读、写、访问数据以及文件名 -rw-r--r-- 1 angus angus 0 2013-08-16 01:33 copy.pl -rw-r--r-- 1 angus angus 1931 2013-08-16 08:27 copy.txt -rw-r--r-- 1 angus angus 492 2013-08-16 03:15 ex.txt -rw-r--r-- 1 angus angus 25 2013-08-16 09:07 hello.txt -rw-r

ls-l

我只需要读、写、访问数据以及文件名

-rw-r--r-- 1 angus angus    0 2013-08-16 01:33 copy.pl
-rw-r--r-- 1 angus angus 1931 2013-08-16 08:27 copy.txt
-rw-r--r-- 1 angus angus  492 2013-08-16 03:15 ex.txt
-rw-r--r-- 1 angus angus   25 2013-08-16 09:07 hello.txt
-rw-r--r-- 1 angus angus   98 2013-08-16 09:05 hi.txt
我编写这段代码是为了提取读、写、访问权限详细信息以及与之对应的文件名。
我无法提取最后一列的文件名

检查perl
stat
它比调用外部
ls
命令更健壮、更高效

#! /usr/bin/perl -w
@list = `ls -l`;
$index = 0;
#print "@list\n";
for(@list){
 ($access) = split(/[\s+]/,$_);
 print "$access\n";
 ($data) = split(/pl+/,$_);
 print "$data";
 @array1 = ($data,$access);
}
print "@array1\n"

检查perl
stat
它比调用外部
ls
命令更健壮、更高效

#! /usr/bin/perl -w
@list = `ls -l`;
$index = 0;
#print "@list\n";
for(@list){
 ($access) = split(/[\s+]/,$_);
 print "$access\n";
 ($data) = split(/pl+/,$_);
 print "$data";
 @array1 = ($data,$access);
}
print "@array1\n"

我想你的脚本第8行有个错误。您正在尝试使用字符串“pl”作为分隔符拆分该行,该分隔符将仅与输入的第一行匹配,并且不会提供我认为您需要的内容

我认为您应该在空白处拆分整行,并只指定所需的列(本例中为数字1和8)

为此更改您的循环:

use File::stat;
$sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
       $filename, $sb->size, $sb->mode & 07777,
       scalar localtime $sb->mtime;

注意:mpapec建议使用Stat会更好。我只是想让你知道为什么你的代码不起作用。

我想你的脚本第8行有一个错误。您正在尝试使用字符串“pl”作为分隔符拆分该行,该分隔符将仅与输入的第一行匹配,并且不会提供我认为您需要的内容

我认为您应该在空白处拆分整行,并只指定所需的列(本例中为数字1和8)

为此更改您的循环:

use File::stat;
$sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
       $filename, $sb->size, $sb->mode & 07777,
       scalar localtime $sb->mtime;

注意:mpapec建议使用Stat会更好。我只是想让你知道为什么你的代码不起作用。

谢谢你的解释谢谢你的解释