in reply to Re^2: search text file
in thread search text file

thank you for your help.i solve it as follow

#!/usr/bin/perl use warnings; use strict; open DOMAINLIST, '<', 'domainlist' or die "Cannot open 'domainlist' be +cause: $!"; chomp( my @list = <DOMAINLIST> ); close DOMAINLIST; open RESULT, '<', 'result' or die "Cannot open 'result' because: $!"; my $domain; #define a hash for count each domain. my %count; while ( my $line = <RESULT> ) { foreach $domain ( @list ) { if ($line =~ /(\Q$domain\E)/g){ $count{$1}++; } } } close RESULT; foreach $domain(keys %count){ print"$domain=$count{$domain}\n"; }

Replies are listed 'Best First'.
Re^4: search text file
by jwkrahn (Abbot) on Jul 26, 2011 at 23:07 UTC

    If you use  if ($line =~ /(\Q$domain\E)/g){ you will only get one $domain per $line.    If you want all $domain per $line then you need to use a while loop:

    my %count; while ( my $line = <RESULT> ) { foreach my $domain ( @list ) { while ( $line =~ /(\Q$domain\E)/g ) { $count{ $1 }++; } } }

    Update: Or better yet, you don't really need capturing parentheses:

    my %count; while ( my $line = <RESULT> ) { foreach my $domain ( @list ) { while ( $line =~ /\Q$domain\E/g ) { $count{ $domain }++; } } }