my $LastSubCat; foreach my $subcat ($category->children) { $LastSubCat = $subcat; # Code to use $subcat } # $LastSubCat now contains the value of the last element