#Might go with a C-style for-loop here. while($j <= $l){ for my $elem (qw(CCMT ...)){ $out .= "," . ($master{$j}{$elem} || '0'); } $j+=60; }