Here's a not-thoroughly-tested | moderately-well-tested recursive approach for any reference to a hash/array-of-hash/array structure of any depth. Whether this is "nice" is perhaps debatable, but at least it avoids a for-loop.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $ComponentData = { 'key_A' => { '6.0' => [ { 'FILENAME' => 'filename', 'VERSION' => 'version', ooo => ' +xxx', }, ], }, }; dd $ComponentData; ;; print highest_multi_element($ComponentData)->{FILENAME}; print highest_multi_element($ComponentData)->{ooo}; my $coderef = sub { return { ooo => 'yyy' }; }; print highest_multi_element($coderef)->{ooo}; ;; sub highest_multi_element { my $dataref = shift; ;; if (ref $dataref eq 'HASH') { my ($single, $val, $multi) = %$dataref; die 'empty hash' unless defined $single; return defined $multi ? $dataref : highest_multi_element($val); } elsif (ref $dataref eq 'ARRAY') { my $items = @$dataref; die 'empty array' unless $items; return $items > 1 ? $dataref : highest_multi_element($dataref->[0 +]); } else { die qq{not hash/array ref: $dataref}; } } " { key_A => { "6.0" => [ { FILENAME => "filename", ooo => "xxx", VERSION => "version" }, ], }, } filename xxx not hash/array ref: CODE(0x66a6bc) at -e line 1.

Update: Changed code example as follows:

I've also had a chance to do more testing and I'm more confident about the robustness of the function.


In reply to Re: Can I avoid loops getting the single value from a hash without knowing the key? by AnomalousMonk
in thread Can I avoid loops getting the single value from a hash without knowing the key? by anadem

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.