Hi,
I have a perl script where I am trying to get a category and subcategories for an item.
I've managed to identify the category and am estimating the subcategories based on the counts of certain occurances. To do this I've created a hash and if certain criteria are met then the count of the value for that particular hash key is incremented.
I therefore have a hash like so:
$subcat{'CLASS1'} = 15
$subcat{'CLASS2'} = 28
$subcat{'CLASS3'} = 7
$subcat{'CLASS4'} = 20
At the end of my function I am trying to output the CLASS keys by sorting on the values descending.
I read that this should work:
sub myreverse { $subcat{$b} <=> $subcat{$a} }
my $j = 0;
my $cat;
for $cat (sort myreverse keys %subcat) {
$prsDetails{'CATEGORY'}{'SUBCATEGORY'.$j}=$cat;
$prsDetails{'CATEGORY'}{'SUBCATEGORY_COUNT'.$j}=$subcat{$cat};
$j++
}
However, when I run it, it actually works but I get the error in the subject line.
Looking around it appears that I need to create an anonymous sub block to fix the issue of the error message. I've tried various things but none have worked for me.
Can anyone please help.
Thanks,
toddyl
I have a perl script where I am trying to get a category and subcategories for an item.
I've managed to identify the category and am estimating the subcategories based on the counts of certain occurances. To do this I've created a hash and if certain criteria are met then the count of the value for that particular hash key is incremented.
I therefore have a hash like so:
$subcat{'CLASS1'} = 15
$subcat{'CLASS2'} = 28
$subcat{'CLASS3'} = 7
$subcat{'CLASS4'} = 20
At the end of my function I am trying to output the CLASS keys by sorting on the values descending.
I read that this should work:
sub myreverse { $subcat{$b} <=> $subcat{$a} }
my $j = 0;
my $cat;
for $cat (sort myreverse keys %subcat) {
$prsDetails{'CATEGORY'}{'SUBCATEGORY'.$j}=$cat;
$prsDetails{'CATEGORY'}{'SUBCATEGORY_COUNT'.$j}=$subcat{$cat};
$j++
}
However, when I run it, it actually works but I get the error in the subject line.
Looking around it appears that I need to create an anonymous sub block to fix the issue of the error message. I've tried various things but none have worked for me.
Can anyone please help.
Thanks,
toddyl