æ·»åãã®2
http://kokogiko.net/m/archives/001966.html
å¥ã«å¼ã°ãã¦ã¸ããã©ããªãããªãããã¯ã¢ã«ã³ã¨æã£ãã
my %analyze = ( 'Pattern 1' => 'Pattern 1', 'Pattern 2' => 'Pattern 2', 'Pattern 3' => 'Pattern 3', .... 'Pattern N' => 'Pattern N', ); my @analyzekey = keys %analyze; my @analyzeval = map { $analyze{$_} } @analyzekey; my $reg = "(?:".join("|",@analyzekey).")"; $reg = qr/$reg/; while (my $log = $logs->readline()) { if (($log->{ua}) && ($log->{ua} =~ /$reg/)) { my ($pattern) = map { $analyzeval[$_-1] } grep { eval "\$$_" } (1..$#analyzeval+1); # Do hoge hoge for $pattern } }
ãããªãã¾ããª
my @analyzekey = keys %analyze; my @analyzeval = map { $analyze{$_} } @analyzekey;
ããããããã©ãªãvalues ããã®ä½¿ã£ããããã¨æã£ã¦ãã
my @analyzeval = values %analyze;
çµæä¸ç·ãã§ã
my $reg = "(?:".join("|",@analyzekey).")";
ãããããã¯ã¡ããããã
ãã¨ã§ãã¿ã¼ã³ããããããã«ã¯å¾æ¹åç
§åºæ¥ãããã«ãã¨ããªãããã®ã¨ã¡ããï¼
(?:Pattern 1|Pattern 3|Pattern N|Pattern 2)
ã£ã¦ãªãã§ã
(?:(Pattern 1)|(Pattern 3)|(Pattern N)|(Pattern 2))
ããããããªãããããã
my $reg = sprintf('(?:%s)', join('|', map { "($_)" } @analyzekey));
ãããªããã¨ããããããã¨ã¡ãããï¼
$reg = qr/$reg/;
ãããªããã¨ã§
$log->{ua} =~ /$reg/
ãããªããã¦ãããã£ããããæå³ãªããã¨ã¡ãããï¼
my ($pattern) = map { $analyzeval[$_-1] } grep { eval "\$$_" } (1..$#analyzeval+1);
ããã¯ãããã®ã¯ã·ããã³ã³ãããã
eval() ã¦éãããã
éãã«ã¼ãã®ä¸ã§ eval() ã¯çµ¶å¯¾ãã£ãããããã¨æãã§ã
(1..$#analyzeval+1);
scalar() 使ãã¨ããªããããã scalar ã³ã³ããã¹ãã§ããã¨è¦ç´ æ°ã«ãªããã
(1 .. @analyzeval);
ã§ãããããã
ãã¨ãä¸è¡ã§æ¸ãã¦ãªããå°ç¶ºéºã«ã¾ã¨ã¾ã£ãæã£ã¦ãããç¥ãããã©ãªã
my ($pattern) =
ä¸åããè¦ç´ ãæãã§ããªãã®ã«ãª
grep { eval "\$$_" } (1..$#analyzeval+1);
ãããè¦ç´ æ°ã¶ãã«ã¼ãã㦠eval() ãã¦ããã¡ãããï¼
ä¸åããå¤ã欲ãããªãã«ããããããã$1 ã true ã«ãªã£ã¦ããN åã¶ãã«ã¼ãããããããã¨ãã¼ã£ã¨ undef ã£ã½ãå¤ã eval() ããããã
ãå®æãªã«ã¼ãããããã¬ãè¨ãã¨ãåããã£ã¦ããã¢ã«ã³ããã
ãã£ãããªããªã
ãããã eval() ã¯ãããã
ãã£ãããã¿ã¼ã³ããããããåç
§é
åãåºæ¥ããããããããã¤ãããããã¨æãã§ã
$reg = qr/$reg/; while (my $log = $logs->readline()) { if ($log->{ua}) { my @jitensha = $log->{ua} =~ $reg; next unless @jitensha; my $pattern; for my $i (0 .. $#jitensha) { if (defined $jitensha[$i]) { $pattern = $analyzeval[$i]; last; } } # Do hoge hoge for $pattern } }
ãããã¨ããããä¸å¿å ¨é¨ã«ã¼ããããã eval() ã¨ããããããããã£ã¨éãããã
ãããè¨ãã¦ãã¯ã·ã®èªè»¢è»ã®éãã«ã¯ããªãããã©ãªã
ãã¼ã大å¡ããã¿ããã«å«ã¯ã欲ãããªã¼ã
â»ãã®ã¨ã³ããªã¯J'sHeadPetã«ããèªåæ稿ã§ã