Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old November 5th, 2000, 05:13 PM
tbonds
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Basically, I'm building a simple shopping cart program that uses a text
file to hold the items a customer adds to his cart. What I want to test
for in my "cart" data file is if the customer has already put the item
in his "cart", meaning that he can't add an item to the cart twice.
(Later, he'll be given a chance to change quantities or remove item at
"checkout" time, so no dupes allowed for now!)

Below is part of the code I'm using to see if an item already exixts in
the cart...meaning there's a line in the cart file for that item
already:

#Add item to shopping cart
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",
hex($1))/eg;
$FORM{$name} = $value;
}

#Read cart file into an arry for processing #in a loop.

open(INF, "<$cart_file") or dienice("Can't open file for reading: $!");
@cart = <INF>;
close (INF);

#Now check to see if one of the array lines #equals what the customer is
#trying to add to his cart.

foreach $line (@cart) {
if ($FORM{'item'} eq $line) {
print "<html><head><title>Cart Contents</title></head>n";
print "<body><h3><center>Item is already in Cart.</center></h3>n";
print "<h2><b><center>$FORM{'item'}</center></b></h2>n";
print "<h2><b><center>$line</center></b></h2>n";
print "</body></html>n";
}
...other code goes here.


My problem with the above snippet of code is that "($FORM{'item'} eq
$line)" NEVER evaluates to true, even when I print out "$FORM{'item'}"
and "$line" to check their values, which appear to be equal. Am I missing a chomp() somewhere,
or do I need a different operator to compare the strings? Any help
would be appreciated as I've run out of ideas. I've searched all over the Web, and checked my Perl books, but to no avail.

Thanks,

Reply With Quote
  #2  
Old November 5th, 2000, 05:58 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>Am I missing a chomp() somewhere

Put it right after the foreach $line (@cart) { line.

chomp $line;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Checking input before writing it to a file.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway