use List::MoreUtils qw/:all/; my $it = natatime 2, `tmsh list net self`; while (my ($addr, $allow_service) = $it->()) { # $addr is the first line, e.g.: address 1.1.1.1/24 # $allow_service is the 2nd line: allow-service all }