Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ok...wrote simple compare script, count how many strings match...

Status
Not open for further replies.

spewn

Programmer
Joined
May 7, 2001
Messages
1,034
here's my script...

Code:
$xError=0;
$checkNm='ogkillabobbyjohnson';
$cfile = "test-supress.txt";
open (COUNTER, "$cfile");
@slist = <COUNTER>;
close (COUNTER);
if ($xError eq 1) {$errorPlural='name';}
else {$errorPlural='names';}
print $cgi->header(-cookie=>$cookie);
foreach $xNm (@slist) {
 if (lc $xNm eq lc $checkNm) {$xError++;}
print $sEmail." - ".$checkNm."<br>";
$x22++;
}
print $xError;

In the test-supress.txt file there is:

Code:
johnwatkins
jimpeters
ogkillabobbyjohnson
betsy ross

even when it comes across a match, it's not making them match and/or counting...

any ideas?

- g
 
The code is a bit confusing, what is $sEmail?
You could have a line feed char at the end of each line


Try -
Code:
foreach $xNm (@slist) {
[red]chomp $xNm[/red]
if (lc $xNm eq lc $checkNm) {$xError++;}

print $sEmail." - ".$checkNm."<br>";
$x22++;
}


Keith
 
Have you deliberately gone out of your way to make life hard for yourself? Have you considered formatting your code a bit to make it easier to see what's going on? And use some sensible variable names, what's with all the x's?


OK, end of rant. Here it is reformatted:
Code:
#!/usr/bin/perl

#use strict; [red](1)[/red]
use warnings;

$xError = 0;
$checkNm ='ogkillabobbyjohnson';
$cfile = "test-supress.txt";

open (COUNTER, "$cfile"); [red](2)[/red]
@slist = <COUNTER>;
close (COUNTER);

chomp @slist; [red](3)[/red]

if ($xError eq 1) {  [red](4)[/red]
   $errorPlural='name';
}
else {
   $errorPlural='names';
}

#print $cgi->header(-cookie=>$cookie); [red](5)[/red]

foreach $xNm (@slist) {
   if (lc $xNm eq lc $checkNm) {
      $xError++;
   }

   print $sEmail." - ".$checkNm."<br>"; [red](6)[/red]
   $x22++; [red](7)[/red]
}
[ol][li]you should use strict and warnings[/li][li]if you aren't using this elsewhere in your script, don't read it into an array[/li][li]variant on Audiopro's chomp[/li][li]why are we setting this here before we've counted anything?[/li][li]had to comment this to get it to work as a fragment[/li][li]why do we print this for every name in the file[/li][li]WTF is this[/li][li]note that at no point do we ever print the contents of $xError[/li][/ol]Here's an alternative, (I've tried to adhere to your corporate naming standards):
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $xError = 0;
my $checkNm ='ogkillabobbyjohnson';
my $cfile = "test-supress.txt";
my $x22;

#print $cgi->header(-cookie=>$cookie);

open (COUNTER, "$cfile") or die "Can't open $cfile: $!";

while (<COUNTER>) {
   $xError++ if (/$checkNm/i);
   $x22++;                      # maybe it has some purpose
}

print $xError, " error", $xError == 1 ? "" : "s", " found for $checkNm\n";

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
LOL@Steve [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top