foreach(@{$arr_ref}) { @hlist=(@$_); foreach my $lines(@hlist){ open (my $file, '>>', 'hpart.txt') or die "Could not open file: $!"; my $output = `my system command`; die "$!" if $?; print $file $output; } }