1. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    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

    #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",
    $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.

  2. #2
  3. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    >>Am I missing a chomp() somewhere

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

    chomp $line;

IMN logo majestic logo threadwatch logo seochat tools logo