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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |