use 5.012; my %hash = ( 0 => 'aa', 1 => 'bb', ... ); my $count = 512; my $_; for (0 .. ($count / 2)) { my $i = 2 * $_; say hex($hash{$i} . $hash{$i + 1}); }