I'm trying to make a hash unique with the following code, is there a way I can optimise this subroutine? The hash has a record_entry identifier as key and the data items are stored in an array, it looks like this:

%comboHashRef = { record_entry <= [0,store variable 1,store variable 2 +, ... product ] record_entry <= [0,product variable1 ,product variab +le2 , ... product ] record_entry <= [1,product variable1 ,product variab +le2 , ... product ] ... }

Using the 2 store variables to specify a unique location, I want to get the unique products to write the whole array for that record_entry to an outfile and count the number of unique, duplicated and total products This needs to be done separately for products that are purchased (1) or not (0). Any thoughts? hidden in-build functions I'm not aware of?

sub uniqify { my $outoutfile = $_[0]; # index of where product is stored in array (can vary) my $pid = $_[1]; my %comboHashRef = %{$_[2]}; my %precords; my @dedup_purchase=(); my @dedup_notpurchase=(); my $product_key; ## loop through hash foreach my $key (keys %comboHashRef){ ## if purchased if ($comboHashRef{$key}->[0] == '1'){ ## create key based on store identifiables my $product_key = $comboHashRef{$key}->[1]."_".$comboHashRef +{$key}->[2]."0"; ## if produce key exists if ($product_key ~~ @dedup_purchase) { ## if product already seen if ($comboHashRef{$key}->[$pid] ~~ $precords{$product_ke +y}{"dedup"}){ $precords{$product_key}{"dup_count"}++; $precords{$product_key}{"total_count"}++; next; ## if product not already seen }else{ push(@{$precords{$product_key}{"dedup"}}, $comboHash +Ref{$key}->[$pid]); $precords{$product_key}{"dedup_count"}++; $precords{$product_key}{"total_count"}++; print $outfile $key ."\t" , (join("\t", @$_), "\n") +for $comboHashRef{$key}; next; } ## if product does not exists }else { push(@dedup_purchase, $product_key); ## create hash for tracking product push(@{$precords{$product_key}{"dedup"}}, $comboHashRef{ +$key}->[$pid]); $precords{$product_key}{"dedup_count"}++; $precords{$product_key}{"total_count"}++; print $outfile $key ."\t" , (join("\t", @$_), "\n") for +$comboHashRef{$key}; next; } ## if not purchased }if ($comboHashRef{$key}->[0] == '0'){ ## create key based on store identifiables my $product_key = $comboHashRef{$key}->[1]."_".$comboHashRef +{$key}->[2].'_-'; ## if key exists if ($product_key ~~ @dedup_notpurchase) { ## if product key aleady seen if ($comboHashRef{$key}->[$pid] ~~ $precords{$product_ke +y}{"dedup"}){ $precords{$product_key}{"dup_count"}++; $precords{$product_key}{"total_count"}++; next; }else{ ## append to hash for tracking product push(@{$precords{$product_key}{"dedup"}}, $comboHash +Ref{$key}->[$pid]); $precords{$product_key}{"dedup_count"}++; $precords{$product_key}{"total_count"}++; print $outfile $key ."\t" , (join("\t", @$_), "\n") +for $comboHashRef{$key}; next; } ## if product does not exists }else{ push(@dedup_notpurchase, $product_key); ## create hash for tracking product push(@{$precords{$product_key}{"dedup"}}, $comboHashRef{ +$key}->[$pid]); $precords{$product_key}{"dedup_count"}++; $precords{$product_key}{"total_count"}++; print $outfile $key ."\t" , (join("\t", @$_), "\n") for +$comboHashRef{$key}; next; } } } return %precords; }

In reply to Optimise Sub, make hash unique based on values in array by anomilie

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.