Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Problem comparing two variables

by blaze (Friar)
on Jan 03, 2003 at 02:17 UTC ( [id://223936]=perlquestion: print w/replies, xml ) Need Help??

blaze has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I am currently writing a shopping cart for my dads floral website, but i'm having some problems with the below chunk of code, this is the entire additem subroutine, the part im having problems with i commented, what its suppose to do is check to see if the item is already in the cart, if it is, just update the quantity instead of adding a duplicate entry...but it just adds a dup entry so i guess its not recognizing that $in is equal to $item for some reason, maybe someone can see my problem, any help is greatly appreciated
sub additem{ my $item = shift; my $quant = shift; if(!(-e $custid)){ open INF, ">$custid" or die "Couldnt create cart file: $!\n"; print INF ''; close INF; } open INF, "products.db" or die "Couldnt open db:$!\n"; my @products = <INF>; close INF; my ($itemno,$prod,$price,$desc); for(@products){ ($itemno,$prod,$price,$desc) = split(/\|/,$_); if($itemno == $item){ last; } } open CUSTCART, "$custid" or die "Couldnt read cart file: $!\n"; my @custchoices = <CUSTCART>; close CUSTCART; for(@custchoices){ chomp; my($in,$pr,$pri,$de,$qu) = split(/\|/,$_); if($in == $item){ #this is the start of my problems **** $qu++; open INF, ">$custid" or die "Couldnt update quantity: $!\n"; print INF "$in|$pr|$pri|$de|$qu|\n"; foreach my $line(@custchoices){ if($item !~ /^$in/){ print INF $line; } } close INF; print "Location: $url=view\n\n"; } } open CUSTID, ">$custid" or die "Couldnt open personal cart: $!\n"; print CUSTID "$itemno|$prod|$price|$desc|$quant|\n"; print CUSTID @custchoices; close CUSTID or die "Couldnt close file: $!\n"; sleep(1); print "Location: $url=view\n\n"; }
products.db looks like:
1002|One Dozen Yellow Roses|59.99|One dozen yellow roses with babys breath in a vase|
1003|One Dozen White Roses|59.99|Perfect for any occasion, one dozen white roses|

$custid is just a file with the contents of the persons shopping cart, which would store exactly as above but add one extra field for quantity

If any additional information is needed just let me know

Thanks,
Robert

Replies are listed 'Best First'.
Re: Problem comparing two variables
by graff (Chancellor) on Jan 03, 2003 at 03:17 UTC
    There are a couple of problems:
    1. You don't want to rewrite the customer's cart file inside the "for (@custchoices)" loop;
    2. After that loop is done, you are printing the current selection along with the existing cart contents, no matter what.

    The "for (@custchoices)" loop should just update a single quantity value if the current item matches something in the cart; if that does not happen, then the current item should be added to the array of cart contents. In either case, you then write the updated or expanded array back to the file as a last step -- something like the following (not entirely tested, but should be close to what you want):

    sub additem { my $item = shift; my $quant = shift; die "You don't want any?? Shame!\n" unless $quant > 0; if(!(-e $custid)){ open INF, ">$custid" or die "Couldnt create cart file: $!\n"; print INF ''; close INF; } open INF, "products.db" or die "Couldnt open db:$!\n"; my @chosen = grep( /^$item\|/, <INF> ); # should return one line close INF; my ($itemno,$prod,$price,$desc) = split( /\|/, $chosen[0] ); open CUSTCART, "$custid" or die "Couldnt read cart file: $!\n"; my @custchoices = <CUSTCART>; close CUSTCART; for(@custchoices){ chomp; my($in,$pr,$pri,$de,$qu) = split(/\|/,$_); if($in == $item){ $qu += $quant; $quant = 0; # this will signal that the item is already i +n the cart last; } } if ( $quant ) { # this is a new item for the cart push @custchoices, join( "|", $itemno, $prod, $price, $desc, $ +quant ); } open CUSTID, ">$custid" or die "Couldnt open personal cart: $!\n"; print CUSTID @custchoices; close CUSTID or die "Couldnt close file: $!\n"; }
    A few extra notes: I have fixed the indentation (I think this is important as a general rule); I have economized a little, using "grep()" to look for the matching product.db record; I added a check to make sure that the "$quant" value is >0, and I add this value to the existing "$qu" value (instead of $qu++) when the item matches an existing cart record.
Re: Problem comparing two variables
by gjb (Vicar) on Jan 03, 2003 at 02:21 UTC

    Are $in and $item numeric? If not, use $in eq $item instead.

    Hope this helps, -gjb-

      yeah, they're both numeric, you can see the whole shebang on my scratchpad

      Thanks tho
      Robert
        you might try doing something like this too.. I use scalar sometimes... like type casting operator...
        for(@custchoices){ chomp; my($in,$pr,$pri,$de,$qu) = split(/\|/,$_); $in = scalar $in; $item = scalar $item; open INF, ">temp.file" or die "$!\n"; print INF "$in\n"; print INF "$item\n"; close INF; if($in == $item){ $qu++; open INF, ">$custid" or die "Couldnt update quantity: $!\n"; print INF "$in|$pr|$pri|$de|$qu|\n"; foreach my $line(@custchoices){ if($item !~ /^$in/){ print INF $line; } } close INF; print "Location: $url=view\n\n"; } }
        :)
Re: Problem comparing two variables
by pg (Canon) on Jan 03, 2003 at 02:45 UTC
    Those facts you provided don't agree with each other. Let's try this, before that if, print $item, and $in, as this:
    print "=$in=\n"; print "=$item=\n";
    This will either help us to figure out the true value of $item and $in, or confirm this is not a problem at all, so we can focus on other places.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://223936]
Approved by pfaut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found