A function which will return an array containing all name/value pairs from a multilevel hash/array... for example if you have:

my %VAR; $VAR{SCALAR} = "test scalar"; $VAR{ARRAY}[0] = "test array"; $VAR{HASH}{NAME}[0] = "test hash array 1"; $VAR{HASH}{NAME}[1] = "test hash array 2"; $VAR{HASH}{NAME}[2]{SOMEHASH} = "test hash array hash 1"; $VAR{HASH}{NAME}[2]{ANOTHERHASH} = "test hash array hash 2";
And pass the function as:

getRefValues( "\$VAR", \%VAR );


Then the return would be an array containing:

$VAR{HASH}{NAME}[0] = (test hash array 1) $VAR{HASH}{NAME}[1] = (test hash array 2) $VAR{HASH}{NAME}[2]{ANOTHERHASH} = (test hash array hash 2) $VAR{HASH}{NAME}[2]{SOMEHASH} = (test hash array hash 1) $VAR{ARRAY}[0] = (test array) $VAR{SCALAR} = (test scalar)
sub getRefValues{ my($name, $ref) = @_;
# A sub that recursively calls itself to drill down a tree of hashes/arrays until all scalar values are found
# The final return is a listing of all absolute name/value pairs

#Usage: 	getRefValues( "nameofrefvar", refvar);
#e.g.	getRefValues("\%VAR", \%VAR );
#	getRefValues("\@VAR", \@VAR );

  my @values;
  
  if ( my $type = ref( $ref  ) )
  {
    if ( $type eq "ARRAY" )
    {
      my $indx=0;
      
      #for each item in the array...
      my $item;
      foreach $item ( @$ref )
      {
        #Check each array item for sub-values, , add the return to the current array of values
	push (@values, getRefValues("$name\$indx\", $item) );
	$indx++;
      }
    }
    elsif ( $type eq "HASH" )
    {
      #For each key in the hash....
      my $key;
      foreach $key ( keys ( %$ref) )
      {
        #Call myself to search for values in current key, add the return to the current array of values
	push (@values, getRefValues( "$name\{$key\}", $$ref{$key} ) );
      }
    }
  }
  else
  {
    #Process ends when we get the final item (a scalar)
    push (@values, "$name = ($ref)") ;
  }
  return @values;
}

In reply to Name/pair values from a multilevel hash/array by darkphorm

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.