检查是否元素在哈希?
好吧,我有以下的散,我试图引用的元素相匹配:
Ok, I have the following hash and am trying to match the referenced element:
$VAR1 = {
'743' => '00:1',
'20687' => '01:3',
};
有人能告诉我,我该如何检查,看看是否值00:1(作为一个例子),另一个变量相匹配?这是我目前已经和不工作:
Could someone tell me how do I check to see if the value '00:1' (as an example) matches another variable? This is what I currently have and is not working:
if($pids{$pid} == qr/$time/){
$pids{$pid}{$time}[$y] = $line;
$y++;
};
我也试过如果(存在($的PID {$ PID} {$}时间))
和其他几个人。
更新
用于修饰下面的问题,是的PID哈希,%的PID。
Pertaining to the questions below, pids is a hash, %pids.
这是哈希表怎么弄出来当我运行它,而不是匹配至$时间,这正是我想要的:
This is how the hash table comes out when I run it without matching to $time which is exactly what I want:
$VAR1 = {
'743' => '00:1',
'4771' => {
'23:5' => [
'Dec 1 23:59:23 ftp1 ftpd[4771]: USER test
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: PASS password
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: CWD /home/test/
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: TYPE Image
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: PASV
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: RETR test
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: QUIT
',
'Dec 1 23:59:23 ftp1 ftpd[4771]: FTP session closed
'
]
},
下面是一个会来通过数据的一个例子:
Here's an example of the data that would come through:
Dec 1 23:59:03 ftp1 ftpd[4771]: USER test
Dec 1 23:59:03 ftp1 ftpd[4771]: PASS password
Dec 1 23:59:03 ftp1 ftpd[4771]: FTP LOGIN FROM 192.168.0.02 [192.168.0.2], test
Dec 1 15:02:03 ftp1 ftpd[4771]: CWD /home/test # Looks same but different time so not a match
Dec 1 23:59:03 ftp1 ftpd[4771]: CWD /home/test
Dec 1 23:59:03 ftp1 ftpd[4771]: TYPE Image
因此,大家可以在上面看到,我可以有两行不同的PID,我只想匹配时代的之一,而非全部。
So as you can see above, I could have two lines with different "pids", I only want to match the one of those times, not both.
如果您需要更多的相关信息,我有另外一个问题的所有细节我都开放,但似乎是死在水中: Playing与在Perl 的
If you need more info on this, I have all the details in another question I have open but seems to be dead in the water: Playing with Hashes from a FTP flow in Perl
如果你可以提取 $时间
每行,这很容易:
If you can extract the $time
from each line, this is easy:
use strict;
use warnings;
use Data::Dump;
my %pids;
while (my $line = <>) {
chomp $line;
my ($time, $pid) = $line =~ /([0-9]{1,2}:[0-9]) .*? \[([0-9]+)\] /x or next;
push @{ $pids{$pid}{$time} }, $line;
}
dd \%pids;
__DATA__
Dec 1 23:59:03 ftp1 ftpd[4771]: USER test
Dec 1 23:59:03 ftp1 ftpd[4771]: PASS password
Dec 1 23:59:03 ftp1 ftpd[4771]: FTP LOGIN FROM 192.168.0.02 [192.168.0.2], test
Dec 1 15:02:03 ftp1 ftpd[4771]: CWD /home/test # Looks same but different time so not a match
Dec 1 23:59:03 ftp1 ftpd[4771]: CWD /home/test
Dec 1 23:59:03 ftp1 ftpd[4771]: TYPE Image
Dec 2 23:12:03 ftp1 ftpd[1234]: USER test
Dec 2 23:12:03 ftp1 ftpd[1234]: PASS password
Dec 2 23:12:03 ftp1 ftpd[1234]: CWD /home/test
这将产生以下的输出:
which would produce the following output:
{
1234 => {
"23:1" => [
"Dec 2 23:12:03 ftp1 ftpd[1234]: USER test",
"Dec 2 23:12:03 ftp1 ftpd[1234]: PASS password ",
"Dec 2 23:12:03 ftp1 ftpd[1234]: CWD /home/test ",
],
},
4771 => {
"15:0" => [
"Dec 1 15:02:03 ftp1 ftpd[4771]: CWD /home/test # Looks same but different time so not a match ",
],
"23:5" => [
"Dec 1 23:59:03 ftp1 ftpd[4771]: USER test",
"Dec 1 23:59:03 ftp1 ftpd[4771]: PASS password ",
"Dec 1 23:59:03 ftp1 ftpd[4771]: FTP LOGIN FROM 192.168.0.02 [192.168.0.2], test ",
"Dec 1 23:59:03 ftp1 ftpd[4771]: CWD /home/test ",
"Dec 1 23:59:03 ftp1 ftpd[4771]: TYPE Image",
],
},
}
如果你只在$ P $感兴趣pdefined设定的PID和时间,你也许可以这样做:
If you are only interested in a predefined set of PIDs and times, you could perhaps do something like:
my %pids = (
5678 => { '00:1' => [] },
4771 => { '23:5' => [] },
);
while (my $line = <>) {
chomp $line;
my ($time, $pid) = $line =~ /([0-9]{1,2}:[0-9]) .*? \[([0-9]+)\] /x or next;
if (exists $pids{$pid} and exists $pids{$pids}{$time}) {
push @{ $pids{$pid}{$time} }, $line;
}
}
或
my %times;
@times{qw/ 00:01 23:5 /} = ();
...
push @{ $pids{$pid}{$time} }, $line if exists $times{$time};
如果没有办法来提取从行时间以一般的方式,可以匹配的所有可能的时间:
If there is no way to extract the time from the line in a general manner, you could match all possible times:
my %pids = (
5678 => { '00:1' => [] },
4771 => { '23:5' => [] },
);
while (my $line = <>) {
chomp $line;
my ($pid) = $line =~ /\[([0-9]+)\]/ or next;
next if not exists $pids{$pid};
my $time_regex = join '|', map quotemeta, keys %{ $pids{$pid} };
my ($time) = $line =~ /($time_regex)/ or next;
push @{ $pids{$pid}{$time} }, $line;
}