while() { @stuff = split(/|/, $_); push(@cats, $stuff[0]); ## Do other stuff with @cats or @stuff, and then: $subcat{$stuff[1]}++; } ## This creates a hash (%subcat) in which the keys are the subcategories, ## and the values are the total of each one. ## So, to output a pretty list: for $x (sort keys %subcat) { print "$x ($subcat{$x})\n"; }