#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595

    Understanding Hashes


    I am trying to reverse engineer a hash but I am don't have enough experience with them to understand what I am seeing. The following is the output from Dumper for my hash called @changes:
    Code:
    $VAR1 = [
              {
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/local',
                'type' => 'stat',
                'change_time' => {
                                 'new' => '1353344714',
                                 'old' => '1352733300'
                               },
                'modification_time' => {
                                       'new' => '1353344714',
                                       'old' => '1312223768'
                                     }
              },
              {
                'uid' => '0',
                'permissions' => '644',
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/local/addedfile2.txt',
                'type' => [
                          'newfile',
                          '100000'
                        ],
                'modification_time' => '1353344714',
                'size' => '0',
                'gid' => '0'
              },
              {
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/bin',
                'type' => 'stat',
                'change_time' => {
                                 'new' => '1353344778',
                                 'old' => '1352733300'
                               },
                'modification_time' => {
                                       'new' => '1353344778',
                                       'old' => '1334360360'
                                     }
              },
              {
                'uid' => '0',
                'permissions' => '755',
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/bin/pinfo.tmp',
                'type' => [
                          'newfile',
                          '100000'
                        ],
                'modification_time' => '1096294431',
                'size' => '100272',
                'gid' => '0'
              },
              {
                'uid' => '0',
                'permissions' => '755',
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/bin/pinky.tmp',
                'type' => [
                          'newfile',
                          '100000'
                        ],
                'modification_time' => '1236179771',
                'size' => '26248',
                'gid' => '0'
              },
              {
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/games',
                'type' => 'stat',
                'change_time' => {
                                 'new' => '1353344685',
                                 'old' => '1352733269'
                               },
                'modification_time' => {
                                       'new' => '1353344685',
                                       'old' => '1092330129'
                                     }
              },
              {
                'uid' => '0',
                'permissions' => '644',
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/games/addedfile.txt',
                'type' => [
                          'newfile',
                          '100000'
                        ],
                'modification_time' => '1353344685',
                'size' => '0',
                'gid' => '0'
              },
              {
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/bin/pgawk',
                'type' => 'stat',
                'change_time' => {
                                 'new' => '1353345281',
                                 'old' => '1352733298'
                               },
                'modification_time' => {
                                       'new' => '1353345281',
                                       'old' => '1099590961'
                                     }
              },
              {
                'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/bin/dmesg',
                'type' => 'stat',
                'change_time' => {
                                 'new' => '1353345307',
                                 'old' => '1352733298'
                               },
                'modification_time' => {
                                       'new' => '1353345307',
                                       'old' => '1236338436'
                                     }
              }
            ];
    I understand how to get the individual elements (e.g. $ref->{type}) but when there is only a single one. It is not clear what I am seeing when there are multiples. I tried treating it like an array in a foreach but that gives me bad index in hash errors.
    Code:
    foreach $change (@changes) {
       print("change type is ".$change->{type}."\n");
    }
    Is my syntax messed up because $change is itself a hash not a scalar or am I not understanding the structure of the hash Dumper is giving me? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    You are using a relatively complex data structure, not a simple hash.

    Try to use Dumper to look at:
    $VAR1->[0], $VAR1->[1], ..., $VAR1->[9].

    For example, $VAR1->[0] will print something like:

    Perl Code:
    0  HASH(0x200bc5f0)
       'change_time' => HASH(0x200bc6b0)
          'new' => 1353344714
          'old' => 1352733300
       'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/local'
       'modification_time' => HASH(0x200bc728)
          'new' => 1353344714
          'old' => 1312223768
       'type' => 'stat'


    So you have an array of hashes.

    But each hash in this array contains some straight forward hash values ('type' and 'file') and also references to nested hashes ('change_time' and 'modification_time' are themselves hashes with a 'new' and an 'old' element).

    You can continue to explore. For example try to print:
    Code:
    $VAR1->[0]{file}
    you should get this:
    Code:
    
    
    /nobackup/fbe/cel5.03-x86_64-1.0/usr/local

    If you print:

    Code:
    $VAR1->[0]{change_time}
    You'll get a reference to a hash, something like this:
    Code:
    HASH(0x200bc6b0)
    Try further to print:
    Code:
    $VAR1->[0]{change_time}{new}
    I hope I have given you an idea on how to explore your data structure, just continue.
    Last edited by Laurent_R; November 19th, 2012 at 02:40 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Thanks. I see my problem, it was the way I referenced elements of the array. The [ ] goes after the -> not before.

    That leaves me with another problem. How do I get the number of elements of the array in that case ($#VAR1 will not work)?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    One additional question, although not really related to the hash issue. Note the time values in the Dumper output (e.g. change_time, modification_time). Can you recognize what they are? If I pass those values to 'localtime' they are completely wrong. They must be some other kind of time stamp but I don't recognize it. TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,264
    Rep Power
    1810
    Originally Posted by gw1500se
    That leaves me with another problem. How do I get the number of elements of the array in that case ($#VAR1 will not work)?
    The scalar keyword is handy.

    Code:
    my $number = scalar @$VAR1;
    
    print "There are $number elements in the array\n";
    If the integer doesn't correspond to localtime, I have no idea. Looks like it would correspond to the filesystem, but it could be UTC.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    The timestamps look OK to me:

    Code:
    $ perl -e '$c = gmtime 1353344714; print "new = $c"'
    new = Mon Nov 19 17:05:14 2012
    $
    $ perl -e '$c = gmtime 1352733300; print "old = $c"'
    old = Mon Nov 12 15:15:00 2012
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    That did it. Thanks.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Originally Posted by Laurent_R
    The timestamps look OK to me:
    Thanks for putting me on the right track. I was just accessing it wrong in the hash.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo