Gossamer Forum
Home : General : Perl Programming :

GREP Count Question

Quote Reply
GREP Count Question
I am having a problem with the grep count.. It keeps putting a 1 after a sentence with the number. I have:

Code:
sub message {
open(COUNT, "$datapath/members/carts/$FORM{'username'}.txt");
print COUNT "$item|$num|$price|$desc|", "\n";
$count = grep { /\n/ + 1 } <COUNT>;
if ($count > 1) {
print "You have $count items in your shopping cart!";
}
else {
print "You have $count item in your shopping cart!";
}
close(COUNT);
}

and then:

Code:
$message = &message;

And then I print it out:
Code:
print "$message";

And say I have 3 items it says:

"You have 3 items in your shopping cart!1"

Why is that 1 in there?
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central

Last edited by:

nolimit: Sep 22, 2001, 5:49 PM
Quote Reply
Re: [nolimit] GREP Count Question In reply to
The 1 is probably what is being returned from message(), and what is being returned by message() is the result from close(COUNT)... which is successful (1).

You should probably change those print statements to $message = "You ... ";
then return $message;


Adrian
Quote Reply
Re: [nolimit] GREP Count Question In reply to
Try:

Code:
sub message {

open(COUNT, "$datapath/members/carts/$FORM{'username'}.txt");
print COUNT "$item|$num|$price|$desc|", "\n";
$count = grep { /\n/ + 1 } <COUNT>;
close(COUNT);

if ($count > 1) {
$message = "You have $count items in your shopping cart!";
}
else {
$message = "You have $count item in your shopping cart!";
}
return $message;

}


Then use:

print &message;

You realise this line is totally insecure?

open(COUNT, "$datapath/members/carts/$FORM{'username'}.txt");

Last edited by:

RedRum: Sep 23, 2001, 4:17 AM
Quote Reply
Re: [RedRum] GREP Count Question In reply to
Thank you both, it works.
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central