my %output_hash; 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]; print "$#{machine_aref} lines returned\n"; foreach my $row ( @{$machines_aref} ) { unless ( defined $row->{$pk} ) { print "'$pk'\n"; print Dumper $row; print "$pk, $row\n"; print "Bad return: Some command from above\n"; print "HERE: " . $row->{"$pk"} . "\n"; die; } $output_hash{$row->{$pk}} = 1; } } my @output_array = keys %output_hash; return \@output_array;