my @output; my $pk = "SomeString"; foreach my $pool ( @pools ) { my @machines = qx/Some command that gets me a list of machines/; #Some code here to check for cmd errors, etc. Convert output to an array of hashes (decode_json is involved); my $machine_aref = decode_json $machines[0]; foreach my $row ( @{$machines_aref} ) { push @output, $row->{$pk}; } } #At this point I can see the full list of machines, with no issues, or empty lines @output = uniq @output; #At this point @output is empty return \@output;