If a hash has only a single value, is there a nice way to get the value without knowing the key and without a foreach loop? The following code illustrates what I've got (with some extra layers, in case any kind monk cares to critique it and help me improve). I need to get the sourcefile for the WinBinary component; it seems a bit weird to use foreach because the "target" key only ever has a single value but can vary between data files. I need to avoid storing the value of the "target" key.
#--------------------- component -- targetver -- Arr -- datahash # # $ComponentData = { # 'key_A' => { # '6.0' => [ # { # 'FILENAME' => 'fi +lename', # 'VERSION' => 've +rsion'; # ooo # }, # ooo # ], # ooo # }, # ooo # } # # where ooo represents potential repetition # my %ComponentData; sub store_data { my $parmref = shift; # input parameter is a hash ref my $component = $parmref->{COMPONENT}; my $tgtver = $parmref->{TGTVER}; my $version = $parmref->{VERSION}; my $file = $parmref->{FILENAME}; # store data $ComponentData{$component}->{$tgtver} = [] unless exists $Componen +tData{$component}->{$tgtver}; my $href = {}; $href->{VERSION} = $version; if( $file ){ $href->{FILENAME} = $file }; push @{$ComponentData{$component}->{$tgtver}}, $href; } sub oldstore_data { # store a data hash $ComponentData{$component}->{$targetver} = [] unless exists $Compo +nentData{$component}->{$targetver}; my $href = {}; if( $file ){ $href->{FILENAME} = $file }; if( $checksum ){ $href->{CHECKSUM} = $checksum }; # etc push @{$ComponentData{$component}->{$targetver}}, $href; return; } # show all the data sub show_all_data { foreach my $comp ( sort keys %ComponentData ) { print "- - - - -\n"; print "component:$comp\n"; foreach my $tgtver ( sort keys %{ $ComponentData{$comp}} ) { print " target:$tgtver\n"; for my $href (@{ $ComponentData{$comp}{$tgtver} }) { print " version:", $href->{VERSION},"\n"; if( $href->{FILENAME} ) { print " file: ", $href->{FILENAME},"\n"; }else{ print " file: undefined\n "; } } print "\n"; } } } while (<DATA>) { chomp; $_ =~ s/ +//g; my ($comp, $tgtver, $version, $file ) = split /\|/; #print "comp=$comp, tgtver=$tgtver, version=$version, file=$file;\ +n"; my $href = {}; $href->{COMPONENT} = $comp; $href->{TGTVER} = $tgtver; $href->{VERSION} = $version; if( $file ){ $href->{FILENAME} = $file; } store_data( $href ); } show_all_data ; # is there a nice way to get the value I want here, without foreach? foreach my $xstgt ( sort keys %{ $ComponentData{ "WinBinary" }} ) { my $specialsource = ${@{ $ComponentData{ "WinBinary" }{$xstgt}}[0] +}{FILENAME}; print "what I want is $specialsource\n" } print "done/n"; # component target version sourcefile __DATA__ WinBinary | 4.0 | 7.8.9.123 | winbinsrc NetBinary | 5.0 | 10.1.2.34 | netbinsrc SupplementalPack | 6.0 | 1.6.0 SupplementalPack | 6.1 | 1.6.0 Hotfix | 6.0 | NULL | hotsource01 Hotfix | 6.0 | NULL | hotsource02 Hotfix | 6.1 | NULL | hotsource03 Hotfix | 6.1 | NULL | hotsource04

In reply to 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.