in reply to Re^2: Question with Dynamically Creating arrays.
in thread Question with Dynamically Creating arrays.
I understand not being able to post the vast majority of a given code, but demonstration code and pseudo-code tends to be worth 1k words.
I'm a little confused by your data structure. You wrote $base{"xx"}[0]{"ls"} (HoAoH) without specifying its value, but it sounds more like you have $base{"xx"}[0] = "ls" (HoA) from your description. For the sake of this discussion, I will sort of split the difference and assume something like $base{"xx"}[0]{"ls"} = "ls xx" (HoAoH).
If you are committed to having an array named @xx, then you need to introduce the new variables you say you are trying to avoid. However, a more natural solution (and fairly close to how I construe your last comment) would be to store the a second hash of arrays (HoA). Something like
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %base = (xx => [{ls => 'ls xx 2>&1'}, {ps => 'ps xx 2>&1'}, ], yy => [{top => 'top yy 2>&1'}, {finger => 'finger yy 2>&1'}, ], ); my %output; for my $key (keys %base) { for my $element (@{$base{$key}}) { my $inner_key = (keys %$element)[0]; my $command = (values %$element)[0]; my $result = `$command`; push @{$output{$key}}, $result; } } print Dumper \%output;
This should at least give you an idea of how to avoid your naming issue. If anything is unclear, I'll be happy to clarify.
|
|---|