... my @Outcond = ([]); #array for out conditions ... sub Outcond { my( $twig, $s )= @_; push(@{$Outcond[$#Outcond]},( $s->att('NAME'))); } ... sub Output { my( $twig, $s )= @_; push(@Jobs,( $s->att('JOBNAME'))); push(@Time,($s->att('TIMEFROM'))); push(@Desc,($s->att('DESCRIPTION'))); push(@Days,($s->att('DAYS'))); push(@Outcond, []); #At the end you will have a trailing empty arrayref at the end of @Outcond, but you don't need to worry about that given the way you print your output } ... sub write_text { my $worksheet = shift; ... #loop all data and output in correct positions while ($count <= (scalar(@Jobs))) { $worksheet->write($ygroup, $xgroup, $group[$count-1]); $worksheet->write($yjobs++, $xjobs, $Jobs[$count-1]); $worksheet->write($yDays++, $xDays, $Days[$count-1]); $worksheet->write($yTags++, $xTags, $Tag[$count-1]); $worksheet->write($yStarting++, $xStarting, $Time[$count-1]); $worksheet->write($yDesc++, $xDesc, $Desc[$count-1]); $worksheet->write($yOut++, $xOut, join(',', @{$Outcond[$count-1]}); $worksheet->write($yIn++, $xIn, $Incond[$count-1]); $ygroup = ($ygroup+$yjobs); $count++; } }