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

thanks for your help,i changed my code but still it doesnt work and i have wrong result.it seems this code dosent search whole file for my each domain.

#!/usr/bin/perl -w use strict; open(DOMAINLIST,'<domainlist') or die,$!; my @list=<DOMAINLIST>; chomp @list; open(RESULT,'<result') or die,$!; while(<RESULT>){ my $domain; my $i=0; my $count=0; for (my $line=$_){ chomp $line; while($i<scalar(@list)){ $domain=$list[$i]; chomp $domain; if (/\Q$domain\E/) { ++$count; } print "$domain\n"; print "$count\n"; ++$i; }} } close RESULT;

Replies are listed 'Best First'.
Re^4: search text file
by jethro (Monsignor) on Jul 26, 2011 at 11:21 UTC

    Think about what you are doing. For every line (the outer loop) you count for every domain, if you find it in that line (the inner loop). You only have one counter ($count) that you reset in the outer loop. That means that your counter only counts occurences in one line (then it gets reset) and it counts all domains together (since it is only one counter

    You have two options:

    The not-really-good option is to reverse the order of the loops again (like you had it in the beginning) and search the result file for every domain one after another. The disadvantage of that method is that you have to reread that result file again and again (for each domain). If the result file is large your simple script will run for minutes or hours and put a lot of strain on your hard disk.

    The better option is to change your second script so that you have more than one counter. In perl this is usually done with hashes. Here the adapted script (I also corrected your use of die):

    #!/usr/bin/perl -w use strict; my %count; open(DOMAINLIST,'<domainlist') or die $!; my @list=<DOMAINLIST>; chomp @list; open(RESULT,'<result') or die $!; while(<RESULT>){ my $domain; my $i=0; for (my $line=$_){ chomp $line; while($i<scalar(@list)){ $domain=$list[$i]; chomp $domain; if (/\Q$domain\E/) { $count{$domain}++; } ++$i; }} } close RESULT; foreach my $domain (keys %count) { print "$domain\n"; print "$count{$domain}\n"; }

    Note that your script still has limitations. Every domain is only counted once per line of the result file.

    PS: You should indent your scripts. Makes them much more readable.