Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2012, 12:24 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 1 h 58 m 34 sec
Reputation Power: 581
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.

Reply With Quote
  #2  
Old November 19th, 2012, 01:11 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 54 sec
Reputation Power: 385
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:
Original - Perl Code
  1. 0  HASH(0x200bc5f0)
  2.    'change_time' => HASH(0x200bc6b0)
  3.       'new' => 1353344714
  4.       'old' => 1352733300
  5.    'file' => '/nobackup/fbe/cel5.03-x86_64-1.0/usr/local'
  6.    'modification_time' => HASH(0x200bc728)
  7.       'new' => 1353344714
  8.       'old' => 1312223768
  9.    '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:
/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 01:40 PM.

Reply With Quote
  #3  
Old November 19th, 2012, 01:31 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 1 h 58 m 34 sec
Reputation Power: 581
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)?

Reply With Quote
  #4  
Old November 19th, 2012, 02:13 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 1 h 58 m 34 sec
Reputation Power: 581
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.

Reply With Quote
  #5  
Old November 19th, 2012, 02:36 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,085 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 51 m 10 sec
Reputation Power: 1809
Quote:
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.

Reply With Quote
  #6  
Old November 19th, 2012, 03:38 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 54 sec
Reputation Power: 385
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

Reply With Quote
  #7  
Old November 20th, 2012, 07:02 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 1 h 58 m 34 sec
Reputation Power: 581
That did it. Thanks.

Reply With Quote
  #8  
Old November 20th, 2012, 07:36 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,879 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 1 h 58 m 34 sec
Reputation Power: 581
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Understanding Hashes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap