Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable "%subcat" will not stay shared 1

Status
Not open for further replies.

toddyl

Technical User
Joined
Sep 26, 2005
Messages
102
Location
US
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
 
Code:
for $cat (sort [b]{ $subcat{$b} <=> $subcat{$a}}[/b] keys %subcat) {

- Kevin, perl coder unexceptional! [wiggle]
 
Excellent stuff.
That worked perfectly.
Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top