in reply to Subtracting one array from another (both have duplicate entries)

You can use a hash to keep track of each letter by setting the value of each key to the number of times it appears in the word and then decrementing that value each time that letter is removed from @letters. Code below creates a new array to hold the remaining letters - probably not the most efficient way but may help:
use strict; use warnings; my @letters = qw(a a a b b b c c c); my @word = qw(a a a b b); # create the hash my %count; for my $letter (@word) { $count{$letter}++ } my @remain; for my $letter (@letters) { if (not $count{$letter}) { push(@remain,$letter); } else { $count{$letter}--; } } print "@remain\n"; # prints: b c c c
  • Comment on Re: Subtracting one array from another (both have duplicate entries)
  • Download Code

Replies are listed 'Best First'.
Re^2: Subtracting one array from another (both have duplicate entries)
by dougbot (Novice) on Dec 04, 2013 at 03:56 UTC
    Awesome! I guess that should have been an obvious way to use a hash for me, but as you can probably tell, I am very much a novice. It is freaking hard learning to write code at this point in my life, but I am pretty sure it good for my brain :-) Thanks again!