http://www.perlmonks.org?node_id=1011893


in reply to can't use certain hashnames when traversing a slightly complex hash

I get to the filename with this:

foreach my $g (@gists) { my @files = keys(%{$g->{files}}); my $file = $files[0]; # $file is now main.html ... }

One final note. The code quoted from the OP works as commented if and only if there is exactly one key in the  'files' sub-hash (and, of course, the name of the key is 'main.html'). If there are two or more keys, the 'order' of keys in the hash is not guaranteed whatsoever, so  'main.html' might be at index 0, index 1, ...

Replies are listed 'Best First'.
Re^2: can't use certain hashnames when traversing a slightly complex hash
by gideondsouza (Pilgrim) on Jan 06, 2013 at 15:46 UTC

    Yep, it's all good. I simplified the code and the hash while posting.

    I'm always interested in the first entry of the files sub-hash. (The rest are forks of the original Github gist.)

    Thanks for looking out :)

      I'm always interested in the first entry of the files sub-hash.

      Hmmm...     Again, and just for the record, the  'files' sub-hash (or any hash) can be said to have a 'first' element if and only if it has exactly one element. The only true ordering in a hash (or associative array) is the association between key/value pairs.

        Ohh ..I understand what you mean now!!!

        Seems github doesn't mentioned specifically whether or files will contain one or more entries. I've emailed them to find out....