use Data::Dumper; my %data; foreach my $index ( 0 .. $#texts ) { push @{$data{ $images[ $index ] }}, $texts[ $index ]; } print Dumper \%data;