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.

In reply to Re: Problem comparing two variables by graff
in thread Problem comparing two variables by blaze

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.