my %buckets; while (my ($k, $v) = each %tapes) { push @{$buckets{substr $v, -2}}, "$k $v\n"; } print @{$bucket{$_}} for sort keys %buckets;