The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Understanding Hashes
Discuss Understanding Hashes in the Perl Programming forum on Dev Shed. Understanding Hashes Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 19th, 2012, 12:24 PM
|
|
|
|
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.
|

November 19th, 2012, 01:11 PM
|
|
|
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 |
|
|
|
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:
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:
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.
|

November 19th, 2012, 01:31 PM
|
|
|
|
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)?
|

November 19th, 2012, 02:13 PM
|
|
|
|
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.
|

November 19th, 2012, 02:36 PM
|
 |
!~ /m$/
|
|
Join Date: May 2004
Location: Reno, NV
|
|
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.
|

November 19th, 2012, 03:38 PM
|
|
|
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
|

November 20th, 2012, 07:02 AM
|
|
|
|
That did it. Thanks.
|

November 20th, 2012, 07:36 AM
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|