作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望遍历我传递给定路径的这些数据结构(基本上是目录结构)。
目标是列出根/基本路径,然后列出所有子 path
s 如果它们存在并且对于每个子 path
存在,列出 file
从那个子路径。
我知道这可能需要遍历 HoH 或 AoH。有人可以向我展示执行此操作的 perl 技术吗?谢谢。
基本级别:/work/eng/feeds
$VAR1 = {
'recursive' => 'no',
'version' => '0.20.202.1.1101050227',
'time' => '2011-10-26T00:20:18+0000',
'filter' => '.*',
'path' => '/work/eng/feeds',
'directory' => [
{
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwxrwxr-x',
'path' => '/work/eng/feeds',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-08-27T03:13:53+0000'
},
{
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwxr-xr-x',
'path' => '/work/eng/feeds/customer_care',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-10-25T23:54:17+0000'
}
],
'exclude' => ''
};
$VAR1 = {
'recursive' => 'no',
'version' => '0.20.202.1.1101050227',
'time' => '2011-10-26T00:21:06+0000',
'filter' => '.*',
'path' => '/work/eng/feeds/customer_care',
'directory' => [
{
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwxr-xr-x',
'path' => '/work/eng/feeds/customer_care',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-10-25T23:54:17+0000'
},
{
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwx------',
'path' => '/work/eng/feeds/customer_care/abc',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-10-25T17:12:56+0000'
},
{
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwx------',
'path' => '/work/eng/feeds/customer_care/def',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-10-25T21:05:50+0000'
},
{
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwx------',
'path' => '/work/eng/feeds/customer_care/test',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-10-25T21:28:14+0000'
}
],
'exclude' => ''
};
$VAR1 = {
'recursive' => 'no',
'version' => '0.20.202.1.1101050227',
'time' => '2011-10-26T00:30:02+0000',
'filter' => '.*',
'file' => {
'owner' => 'tst_act',
'replication' => '3',
'blocksize' => '134217728',
'permission' => '-rw-------',
'path' => '/work/eng/feeds/customer_care/test/q_data_20111023.dat',
'modified' => '2011-10-26T00:29:46+0000',
'size' => '379085',
'group' => 'eng',
'accesstime' => '2011-10-26T00:29:46+0000'
},
'path' => '/work/eng/feeds/customer_care/test',
'directory' => {
'owner' => 'tst_act',
'group' => 'eng',
'permission' => 'drwx------',
'path' => '/work/eng/feeds/customer_care/test',
'accesstime' => '1970-01-01T00:00:00+0000',
'modified' => '2011-10-26T00:29:46+0000'
},
'exclude' => ''
};
最佳答案
这是一个启动器:
sub list_path_files {
my ($data) = @_;
say $data->{path}; # get value from a hashref
my @directories;
# check whether it is a single value or an arrayref of values
if (ref $data->{directory} eq 'ARRAY') {
@directories = @{ $data->{directory} }; # dereference the arrayref to get an AoH
} else {
@directories = $data->{directory}; # just get the single value
}
for my $dir (@directories) {
next if $dir->{path} eq $data->{path};
say $dir->{path};
}
# I'll leave the rest for you to do
}
each
,
keys
或
values
职能:
%hash = %$VAR1; # dereference
while (my ($key, $value) = each %hash) {...}
for my $key (keys %$VAR1) {
my $value = $VAR1->{$key};
}
for my $value (values %$VAR1) {...}
if (ref $val eq '') {
# $val is just a scalar - don't need to deref
}
elsif (ref $val eq 'HASH') {
my %hash = %$val;
}
elsif (ref $val eq 'ARRAY') {
my @array = @$val;
}
关于arrays - 遍历数据数组/哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7897669/
我是一名优秀的程序员,十分优秀!