in reply to Question with Dynamically Creating arrays.

Given that you are handling a complex data structure in Perl (perldsc, perllol, perlreftut) why do you feel the need to have the values stored in another array? Assuming you have a hash %base that is perhaps a deference of the hash ref returned from your JSON parse, you could access deep data with syntax similar to $base{xx}[1]{key1} (the value corresponding to key1 in the second referenced array corresponding to xx). You can loop over all these structures, store local copies of the arrays/hashes using references to reduce boiler plate and anything else you'd normally do in Perl.

You might also find a read through of Why it's stupid to use a variable as a variable name enlightening, though it is a little off point as you are (thankfully) not using symbolic references.

I'd love to give more concrete advice, but without any source code to critique (How do I post a question effectively?) I'm limited in suggestions. Post some source code, and we'll be happy to give you some ideas on how to improve it (subjective advice, of course).

Replies are listed 'Best First'.
Re^2: Question with Dynamically Creating arrays.
by abhijithtk (Novice) on Jul 21, 2010 at 17:59 UTC

    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..

      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.

      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.

      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.
Re^2: Question with Dynamically Creating arrays.
by abhijithtk (Novice) on Jul 21, 2010 at 18:57 UTC

    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..

      sorry. i reposted it by mistake.. will have a look at your replies.
      Thanks