foreach my $key ( keys %ifSpeeds ) { if ($counter <= 8) { print "$key "; $counter++; } else { print "$key\n"; # <-------------- $counter = 1; } } #### use List::MoreUtils qw(natatime); my $it = natatime(9, (keys %ifSpeeds)); while (my @vals = $it->()) { print "@vals\n"; }