English 中文(简体)
我怎么知道我的数组中是否有类似的元素?
原标题:How would I know if there is any similar element in my array?
  • 时间:2011-02-16 01:24:17
  •  标签:
  • arrays
  • perl

我有这样的代码,它列出了我目录中的所有文件:

$dir =  /var/www/corpIDD/rawFile/ ;
opendir DIR, $dir or die "cannot open dir $dir: $!";
my @file= readdir DIR;
closedir DIR;

它返回一个包含如下内容的数组:

$array (0 =>  ipax3_2011_01_27.txt , 1 =>  ipax3_2011_02_01.txt , 2 =>  ipax3_2011_02_03.txt )

我的问题是,我将如何存储元素1=>;ipax3_2011_02_01.txt和2=>;ipax3_2011_02_03.txt来分隔变量,因为它们属于同一月份和年份(2011_02)?

谢谢

最佳回答

在Perl中,当您需要使用字符串作为数据结构中的键时,您正在寻找HASH内置类型,由%sigil指定。Perl散列的一个很好的特性是,您不必预先声明复杂的数据结构。您可以使用它,Perl将根据该用法推断出结构。

my @file = qw(ipax3_2011_01_27.txt ipax3_2011_02_01.txt ipax3_2011_02_03.txt);

my %ipax3;

for (@file) {
    if (/^ipax3_(d{4}_d{2})_(d{2}).txt$/) {
        $ipax3{$1}{$2} = $_
    }
    else {
        warn "bad file: $_
"
    }
}

for my $year_month (keys %ipax3) {
    my $days = keys %{ $ipax3{$year_month} };
    if ($days > 1) {
        print "$year_month has $days files
";
    }
    else {
        print "$year_month has 1 file
";
    }
}

其打印:

2011_01 has 1 file
2011_02 has 2 files

要查看各个文件,请执行以下操作:

my $year_month =  2011_02 ;
my $day        =  01 ;
my $file       = $ipax3{$year_month}{$day};

上面我使用keys函数的返回值作为要迭代的列表和天数。这是可能的,因为<code>键

my $number = keys %ipax3; # number of year_month entries

my @keys = keys %ipax3; # contains ( 2011_01 ,  2011_02 ) 

my @days = keys %{ $ipax{$year_month} };

在最后一个示例中,%ipax中的每个值都是对哈希的引用。由于keys采用文字哈希,因此需要将$ipax{$year_month}封装在%{…}中。在perl v5.13.7+中,您可以省略%{…}围绕的参数以及其他一些数据结构访问函数。

问题回答

人们在这里的反应真的很快:)无论如何,我会发布我的,仅供参考。基本上,我也在使用散列。

use warnings qw(all);
use strict;

my ($dir, $hdir) =  C:Work ;
opendir($hdir, $dir) || die "Can t open dir "$dir" because $!
";
my (@files) = readdir($hdir);
closedir($hdir);

my %yearmonths;
foreach(@files)
{
    my ($year, $month);
    next unless(($year, $month) = ($_ =~ /ipax3_(d{4})_(d{2})/));
    $year += 0;
    --$month;           #assuming that months are in range 1-12
    my $key = ($year * 12) + $month;
    ++$yearmonths{ $key };
}
foreach(keys %yearmonths)
{
    next if($yearmonths{ $_ } < 2);
    my $year = $_ / 12;
    my $month = 1 + ($_ % 12);
    printf "There were %d files from year %d, month %d
", $yearmonths{$_}, $year, $month;
}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签