Now i have encountered a problem.....They would have to manually edit the code to create an array called @aa to store its values.

If I understood this well enough, you will add the keys manually to the hash right?, you can do that directly without having to explicitly create an array every time to hold the values for you. That other array holding the key names can be dynamically updated for hash keys by simply assigning to the array the keys of the hash using the keys function...

#!/usr/local/bin/perl use strict; use warnings; my %hash; my @array_keys; my @keys = qw(xx yy zz); @hash{@keys}=([{'key1'=>1}],[{'key2'=>2}],[{'key3'=>3}]); #hash slice @array_keys = keys %hash; print "@array_keys\n"; push @{$hash{'aa'}},{'key4'=>4}; #add another key @array_keys = keys %hash; #update the array print "@array_keys\n";
UPDATE:This post is a response to the OP original post which did not bear a mention of the requirement that he presented in the response to kennethk reply to the same post.. It is possible however to integrate further approaches towards achieving the OP's desired goal, for example:
for (@array_keys){ for my $element ($hash{$_}){ my @val; for my $element1 ($element->[0]){ @val = values %$element1; } print "@val\n"; } } #OR @array_values = values %hash; for (@array_values){ for my $element ($_){ my @val; for my $element1 ($element->[0]){ @val = values %$element1; } print "@val\n"; } }
The following code is still welcoming improvement...
use strict; use warnings; use Data::Dumper; my %hash; my @array_values; my @keys = qw(xx yy zz); @hash{@keys}=([{'key1'=>'ipconfig'}],[{'key2'=>'dir'}]); @array_values = values %hash; my %hash_output; for (@array_values){ for my $element ($_){ for my $element1 ($element->[0]){ for my $result (values %$element1){ my $outcome = `$result`; push @{$hash_output{command}[0]}, $out +come; } } } } print Dumper(\%hash_output);


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

In reply to Re: Question with Dynamically Creating arrays. by biohisham
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.