title: How to access a hash value by index stored in variable
body: <p>I am working on some code to extract data stored in JSON format from web page. JSON data extracted and decode properly into hash. </p>
  
  <p>The structure of JSON data is very complex, I wrote some assisting code/function which 'traverse' through the hash and helps finds 'index' (location) of a value of interest in the hash. </p>
  
  <p>The 'find' function returns 'index' (location) of the data in the hash which stored in a variable.</p>
  
  <p>I attempt to use this variable (stored 'index') in other operations but so far without any success.</p>
  
  <p>Please see included simple demo snippet code for explanation of the problem.</p>
  
  <p>Thank you,
  Polar Bear  </p>
  
  <pre><code>use strict;
  use warnings;
  
  use JSON qw(decode_json);
  
  my $index;
  my $slice;
  
  my $data = decode_json( join '', &lt;DATA&gt; );
  
  printf "TITLE: %-15s TIME: %5s TIMES: %5s FAVORITE: %s\n",
          $data-&gt;{playList}[1]{title},
          $data-&gt;{playList}[1]{time},
          $data-&gt;{playList}[1]{played},
          $data-&gt;{playList}[1]{favorite} ? "yes" : "no";
  
  $index = '{playList}[1]';
  
  $slice = $data{$index};     # does not pass 'use strict' compilation error
  $slice = $data-&gt;{$index};   # empty slice 
  $slice = $data-&gt;$index;     # Can't call method "{playList}[1]" on unblessed reference at
  
  printf "TITLE: %-15s TIME: %5s TIMES: %5s FAVORITE: %s\n",
          $slice-&gt;{title},
          $slice-&gt;{time},
          $slice-&gt;{played},
          $slice-&gt;{favorite} ? "yes" : "no";
  
  __DATA__
  {
      "playList": [
        {
          "title": "Song name 1",
          "time": "3:25",
          "played": "240",
          "favorite": "1"
        },
        {
          "title": "Song name 2",
          "time": "4:12",
          "played": "30",
          "favorite": "0"
        },
        {
          "title": "Song name 3",
          "time": "2:56",
          "played": "85",
          "favorite": "0"
        }
      ]
  }
  </code></pre>
  
  <p>I expect to access data by utilizing 'index' stored in a variable but I could not find a way to achieve this result. See comments in the code for details.</p>
  
  <p>NOTE: In real life the index looks as following</p>
  
  <pre><code>my $index = "{contents}{twoColumnBrowseResultsRenderer}{tabs}[0]{tabRenderer}{content}{sectionListRenderer}{contents}[0]{itemSectionRenderer}{contents}[0]{playlistVideoListRenderer}{contents}[0]{playlistVideoRenderer}{title}{accessibility}{accessibilityData}{label}";
  </code></pre>
  
  <p>SOLUTION:</p>
  
  <p>I would like to extend <strong>my '<em>thank you</em>'</strong> to Håkon Hægland and lordadmira for offered solution</p>
  
  <pre><code>use Data::Diver qw/Dive/; # or Data::DPath, etc
  
  # Capture web page, extract data JSON, convert to hash, assign hash ref to $data
  my $data = ...; 
  
  # Find index/location in the hash
  #my $index = find($data, $value);
  
  my $index = "{contents}{twoColumnBrowseResultsRenderer}{tabs}[0]{tabRenderer}{content}{sectionListRenderer}{contents}[0]{itemSectionRenderer}{contents}[0]{playlistVideoListRenderer}{contents}[0]{playlistVideoRenderer}{title}{accessibility}{accessibilityData}{label}";
  
  $index =~ s/[{\[]//g; # throw away opening brackets
  
  my @index = split /[}\]]/, $index; # split into array on closing brackets
  
  pop @index for 1..8 # 8 levels to back up to
  
  my $slice = Dive( $data, @index ); # extract hash slice of interest
  
  # extract playlist
  my $playlist = $slice-&gt;{playlistVideoListRenderer}{contents};
  
  # go through playlist and extract information of our interest
  foreach ( @$playlist ) {
      my $video = $_-&gt;{playlistVideoRenderer};
      printf "%s %8s %s\n",
              $video-&gt;{videoId},
              $video-&gt;{lengthText}{simpleText},
              $video-&gt;{title}{simpleText};
  }
  </code></pre>
  
  <p>Both of them referred me to <code>use Data::Dive</code> with help of this module I can do <em>back up</em> few levels from the depth of the hash and extract <strong>slice</strong> of interest.</p>
  
  <p>It was learned that by utilizing this module the index in form of <strong>array</strong> is easier to work with. Due this factor I will <em>alter</em> my find function to return an index <strong>array</strong>.</p>