#! perl use strict; use warnings; use Data::Dump; my @A = qw{H1 H2 H3 H4 H5}; my @B = 1 .. 23; my %h; while (@B) { push @{ $h{$_} }, shift @B // () for @A, reverse @A; } dd \%h;