perlperl-data-structures

Perl - traversing hash of (hashes of hashes and scalars)?


I have generated a hash that looks like this. (printed using Dumper)

$VAR1 = {
          'sufia:s4655g60k' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '44d88612fea8a8f36de82e1278abb02f',
                                                  'checksum.old' => '44d88612fea8a8f36de82e1278abb02f',
                                                  'name' => 'eicar.com.txt',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/47/info%3Afedora%2Fsufia%3As4655g60k%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z9031393' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => 'b502be8d9e1286c94506c91c191c0b5a',
                                                  'checksum.old' => 'b502be8d9e1286c94506c91c191c0b5a',
                                                  'name' => 'Elsevier RightsLink Printable License.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/d9/info%3Afedora%2Fsufia%3A9z9031393%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:s4655g65z' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '1baaa38b3e7b6139b3847b7bb3b8644a',
                                                  'checksum.old' => '1baaa38b3e7b6139b3847b7bb3b8644a',
                                                  'name' => 'enso-crystal.jpg',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/3a/info%3Afedora%2Fsufia%3As4655g65z%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:d504rk35q' => {
                                 'version.2' => {
                                                  'checksum.new' => '9e5422337c33fa83322fd287035e4ac5',
                                                  'checksum.old' => '9e5422337c33fa83322fd287035e4ac5',
                                                  'name' => '0501012.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/c4/info%3Afedora%2Fsufia%3Ad504rk35q%2Fcontent%2Fcontent.2'
                                                },
                                 'curVer' => 2,
                                 'version.1' => {
                                                  'checksum.new' => 'a50e552f4776f1863050b89920c9f7eb',
                                                  'checksum.old' => 'a50e552f4776f1863050b89920c9f7eb',
                                                  'name' => '0501012.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/d1/info%3Afedora%2Fsufia%3Ad504rk35q%2Fcontent%2Fcontent.1'
                                                },
                                 'version.0' => {
                                                  'checksum.new' => 'b05398d3071808b880af4375f6cdf18d',
                                                  'checksum.old' => 'b05398d3071808b880af4375f6cdf18d',
                                                  'name' => '0501012.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/80/info%3Afedora%2Fsufia%3Ad504rk35q%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903124g' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => 'cd022d22cb5c46bf0e3f6c3b4c929ae1',
                                                  'checksum.old' => 'cd022d22cb5c46bf0e3f6c3b4c929ae1',
                                                  'name' => 'GT Contributor - Consent to Publish 2015-LindaNewman.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/04/info%3Afedora%2Fsufia%3A9z903124g%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903137j' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => 'c2a24f16cb62d765810af770510516d1',
                                                  'checksum.old' => 'c2a24f16cb62d765810af770510516d1',
                                                  'name' => 'ScholarAtUC-SplashPage-Revisions4-29-3F.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/99/info%3Afedora%2Fsufia%3A9z903137j%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903133f' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '9d1a7f558548815dbb8be639c315f367',
                                                  'checksum.old' => '9d1a7f558548815dbb8be639c315f367',
                                                  'name' => 'UCScienceNet Expansion Award Proposal-Isilon-To-APTrust.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/70/info%3Afedora%2Fsufia%3A9z903133f%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z9031279' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '6e763df35f2d807eac2763a87c6bb0bb',
                                                  'checksum.old' => '6e763df35f2d807eac2763a87c6bb0bb',
                                                  'name' => 'IMG_2221.JPG',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/52/info%3Afedora%2Fsufia%3A9z9031279%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z9031414' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '621a177b09602ce1f703dd4becd90fe8',
                                                  'checksum.old' => '621a177b09602ce1f703dd4becd90fe8',
                                                  'name' => 'MazdaLiveAssistance-Not.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/0d/info%3Afedora%2Fsufia%3A9z9031414%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903122x' => {
                                 'curVer' => 0,


                                 'version.0' => {
                                                  'checksum.new' => 'a6653aac025f0eccbc5f96226923c79a',
                                                  'checksum.old' => 'a6653aac025f0eccbc5f96226923c79a',
                                                  'name' => 'example.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/41/info%3Afedora%2Fsufia%3A9z903122x%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:s4655g624' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '44d88612fea8a8f36de82e1278abb02f',
                                                  'checksum.old' => '44d88612fea8a8f36de82e1278abb02f',
                                                  'name' => 'NotAVirusIPromise.txt',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/c6/info%3Afedora%2Fsufia%3As4655g624%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903131w' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => 'f8f82d7f4d7a20839a20a9dd375f6478',
                                                  'checksum.old' => 'f8f82d7f4d7a20839a20a9dd375f6478',
                                                  'name' => 'UCScienceNet Expansion Award Proposal-LangsamLibrary.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/8d/info%3Afedora%2Fsufia%3A9z903131w%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903152n' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => 'e5c6bde4b0fa6c4f2e013656f6aa8bf5',
                                                  'checksum.old' => 'e5c6bde4b0fa6c4f2e013656f6aa8bf5',
                                                  'name' => '20950 dod.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/c9/info%3Afedora%2Fsufia%3A9z903152n%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903118b' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '99ada40eb1ad4c5f5bffe07108855dc6',
                                                  'checksum.old' => '99ada40eb1ad4c5f5bffe07108855dc6',
                                                  'name' => 'harvey1880.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/4d/info%3Afedora%2Fsufia%3A9z903118b%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903144z' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => 'a6653aac025f0eccbc5f96226923c79a',
                                                  'checksum.old' => 'a6653aac025f0eccbc5f96226923c79a',
                                                  'name' => 'example.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/f4/info%3Afedora%2Fsufia%3A9z903144z%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z9031350' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '23013ce2bb7203fff545cf6e9ec5ac4a',
                                                  'checksum.old' => '23013ce2bb7203fff545cf6e9ec5ac4a',
                                                  'name' => 'ScholarAtUC-SplashPage-Revisions4-29-1D.pdf',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/6e/info%3Afedora%2Fsufia%3A9z9031350%2Fcontent%2Fcontent.0'
                                                }
                               },
          'sufia:9z903129v' => {
                                 'curVer' => 0,
                                 'version.0' => {
                                                  'checksum.new' => '93fd70923362e02b4fb56bb137bcf1e2',
                                                  'checksum.old' => '93fd70923362e02b4fb56bb137bcf1e2',
                                                  'name' => 'DevTeam2.jpg',
                                                  'path' => '/opt/rails-apps/curate_app/jetty/fedora/default/data/datastreamStore/94/info%3Afedora%2Fsufia%3A9z903129v%2Fcontent%2Fcontent.0'
                                                }
                               }
        };

Have looked at How to iterate through Hash (of Hashes) in Perl? and couldn't get it work for me.

Need to traverse through all the keys that look like 'sufia:s4655g60k' and then for each of them, I need to extract 'curVer' and depending on the number of 'curVer' I need to cycle through 'version.0' to get the respective values of the keys.

How do I traverse this hash and read/update existing keys/values?


Solution

  • for my $key (keys(%$VAR1)) {
       my $rec = $VAR1->{$key};
    
       my $cur_ver = $rec->{curVer};
       my $subrec  = $rec->{"version.$cur_ver"};
    
       my $checksum_new = $subrec->{'checksum.new'};
       my $checksum_old = $subrec->{'checksum.old'};
       my $name         = $subrec->{name};
       my $path         = $subrec->{path};
    
       ...
    }