in reply to Re: Question with Dynamically Creating arrays.
in thread Question with Dynamically Creating arrays.

Hello Kenneth,

Im sorry but i will not be able to post any code. I hope you understand. :(

I will give u a clearer picture of what i am doing, and what i want. I hope it helps.

I am able to access the elements using the hash ref. like what u said. $base{$key}index{$inner_key} is what i have done.

For Eg;

$base{$key}index{$inner_key} contains shell commands, like ls, ps, who, etc etc.

I want to be able to store the outputs of each of these commands ( $inner_key ) in an array which is specific to the outer hash key. ( $key ).

like $base{"xx"}[0]{"ls"} $base{"xx"}1{"ps"}

$base{"yy"}[0]{"top"} $base{"yy"}1{"finger"}

i want each of those outputs to be stored in an array called @xx and @yy where $xx[0] = "output of ls" $xx1 = "output of ps"

$yy[0] = "output of top" $yy1 = "output of finger"

Is there a way i can handle this independent of the $key value/name.

i was thinking of creating something on the lines of $somestruct{$key} = [] // anonymous array. and then i can push or pop values into the anony array. I have no idea if what im asking is even valid. Im actually confused now..

  • Comment on Re^2: Question with Dynamically Creating arrays.

Replies are listed 'Best First'.
Re^3: Question with Dynamically Creating arrays.
by kennethk (Abbot) on Jul 21, 2010 at 18:24 UTC
    Please wrap code in <code> tags - see Writeup Formatting Tips. Because you did not wrap your code in code tags, many of your [ ] pairs indicating array indices were linkified.

    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.

Re^3: Question with Dynamically Creating arrays.
by dasgar (Priest) on Jul 21, 2010 at 18:37 UTC

    What you're now describing sounds like a dangerous path to go down, especially as far as debugging and code maintenance is concerned. At least it would be for me. :)

    Based on you new post, it sounds like you have one complex data structure that is storing commands and you want store the output of those commands. If that's correct, why not store both the command and output in the same data structure? (See example code, based on you last post, below)

    my %base; $base{"xx"}[0]{"command"} = "ls"; $base{"xx"}[0]{"output"} = `$base{"xx"}[0]{"command"}`;

    If you go this route, you'll avoid using a variable's value as the name of a variable. Plus, if you use a module like Data::Dumper when debugging, it'll help you see easily and quickly what the command was along with it's output. Hope this helps.

Re^3: Question with Dynamically Creating arrays.
by Anonymous Monk on Jul 21, 2010 at 18:35 UTC
    Why do you want to go through so much trouble to avoid putting the output in a similarly configured hash?  $output{xx}[1] seems like a small price to pay to avoid using the symbolic references you seem to be talking yourself into.