This feels like the wrong solution to the problem as the results are very messy. This seems like a good situation for a struct type solution:

use strict; use warnings; # make a little mini class for your records use Class::Struct Record => [title => '$', rule_severity => '$', rule_ +weight => '$' ]; my %data_hash = (); my @data_array = (); $data_hash{first_key} = new Record; $data_hash{second_key} = new Record; # instead of pushing a text string of keys, just push field to access push(@data_array, 'title'); push(@data_array, 'rule_weight'); # some code later changes the hash $data_hash{first_key}->title('Mr. President'); $data_hash{first_key}->rule_weight(100); $data_hash{second_key}->title('Emperor'); $data_hash{second_key}->rule_weight(1000); # want to access it from your array for my $key (sort keys %data_hash) { for my $field (@data_array) { print "My data is (".$data_hash{$key}->$field.")\n"; } }

Output:

C:\tmp>tmp.pl My data is (Mr. President) My data is (100) My data is (Emperor) My data is (1000)

In reply to Re: Storing an indeterminate number of hash keys in a variable by tj_thompson
in thread Storing an indeterminate number of hash keys in a variable by banesong

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.