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

Try it like this:

#!/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: $!"; while ( my $line = <RESULT> ) { for my $domain ( @list ) { ++$count while $line =~ /$domain/g; } } close RESULT; print "$count\n";

Replies are listed 'Best First'.
Re^3: search text file
by Anonymous Monk on Jul 26, 2011 at 14:47 UTC

    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"; }

      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 }++; } } }
Re^3: search text file
by Anonymous Monk on Jul 26, 2011 at 12:25 UTC

    i want count of each domain in @list separately.thank you.