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\|/, ); # 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 = ; 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 in 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"; }