sub build_huffman_tree { my $input=shift; my %count; $count{$_}++ for split //,$input; my @list=map { [ $_, $count{$_} ] } keys %count; while (@list>1) { @list=sort {$a->[1] <=> $b->[1]} @list; my ($x,$y)=splice @list,0,2; push @list,[[$x,$y],$x->[1]+$y->[1]]; } return wantarray ? @list : $list[0]; }