in reply to Problem comparing two variables

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.