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.


In reply to Re^3: Question with Dynamically Creating arrays. by kennethk
in thread Question with Dynamically Creating arrays. by abhijithtk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.