my @tree_array; # my @species = split "//", $list; # would: # foreach my species ('a','b','c') {} work? I think not? foreach my $species(@species) { my @tree = edit($species); push @tree_array, [@tree] if @tree; print @tree if @tree; } ## do you want to use @tree here? ## if so, then ...you need to use the array ## of the @tree results... foreach $tree_ref (@tree_array) { print "@$tree_ref\n"; } sub edit { my ($species) = shift; #slightly faster when just one arg my ($pid) = ($species =~ /RANK.*?subspecies.*?PARENT ID.*?(\d+)/); ### or ### ## look for "PARENT ID", then anything until there are ## one or more digits my ($pid) = ($species =~ /PARENT ID.*?(\d+)/); if (defined $pid) { return `getz "[taxonomy:$pid]" -e`; } return undef; }