varneraa has asked for the wisdom of the Perl Monks concerning the following question:
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;
Same loop to generation to generate the arrays. Still seeing all value +s in the array before this: my @final_output; foreach my $line ( @output ) { next if ( grep m/^$line$/ @final_output ); push @final_output, $line; } <\code> <br> This time I don't see 0 results, the code just hangs. At first I thoug +ht, wow, is it really that inefficient? So I decide I can do better.< +br> <br> From a little reading about uniq I find out it essentially just slams +the array values into a hash and then pulls the keys. I don't care ab +out ordering, so why not just roll that myself?<br> <br> <code> 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]; foreach my $row ( @{$machines_aref} ) { $output_hash{$row->{$pk}} = 1; } } my @output_array = keys %output_hash; return \@output_array;
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;
<PoolName>: 4737 lines returned. 'SomeString' $VAR1 = { 'SomeString' => '<machinename>.<location>.<something>.com', 'Count' => 6 }; SomeString, HASH(0x106d978) Bad command return: <Some command used to get this data> Use of uninitialized value in concatenation (.) or string at <script_p +ath> line <Line with print "HERE: " .$row->{"$pk"} . "\n";>.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Dangers of decode_json? or a hash entry is both defined and undefined. WTF?
by Haarg (Priest) on Jun 02, 2020 at 08:10 UTC | |
Re: Dangers of decode_json? or a hash entry is both defined and undefined. WTF?
by perlfan (Parson) on Jun 02, 2020 at 04:46 UTC | |
Re: Dangers of decode_json? or a hash entry is both defined and undefined. WTF?
by haukex (Archbishop) on Jun 02, 2020 at 21:49 UTC |