1

Perl newbie here...I had help with this working perl script with some HASH code and I just need help understanding that code and if it could be written in a way that I would understand the use of HASHES more easily or visually??

In summary the script does a regex to filter on date and the rest of the regex will pull data related to that date.

use strict;
use warnings;
use constant debug => 0;
my $mon = 'Jul';
my $day = 28;
my $year = 2010;
my %items = ();

while (my $line = <>)
{
    chomp $line;
    print "Line: $line\n" if debug; 
    if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([a-zA-Z0-9._]*):.*/)
    {
        print "### Scan\n" if debug;
        my $date = $1;
        my $set = $2;
        print "$date ($set): " if debug;
        $items{$set}->{'a-logdate'} = $date;
        $items{$set}->{'a-dataset'} = $set;
        if ($line =~ m/(ERROR|backup-date|backup-size|backup-time|backup-status)[:=](.+)/)
        {
            my $key = $1;
            my $val = $2;
            $items{$set}->{$key} = $val;
            print "$key=$val\n" if debug;
        }
    }
}

print "### Verify\n";
for my $set (sort keys %items)
{
    print "Set: $set\n";
    my %info = %{$items{$set}};
    for my $key (sort keys %info)
    {
        printf "%s=%s;", $key, $info{$key};
    }
    print "\n";
}

What I am trying to understand is these lines:

        $items{$set}->{'a-logdate'} = $date;
        $items{$set}->{'a-dataset'} = $set;

And again couple lines down:

        $items{$set}->{$key} = $val;

Is this an example of hash reference? hash of hashes?
I guess i'm confused with the use of {$set} :-(

jdamae
  • 3,839
  • 16
  • 58
  • 78
  • 2
    If you have not read [perldoc perlreftut](http://perldoc.perl.org/perlreftut.html) and [perldoc perldsc](http://perldoc.perl.org/perldsc.html) they would be good places to start. – hobbs Aug 03 '10 at 23:42
  • @hobbs - thanks, there are some good examples there. – jdamae Aug 04 '10 at 02:42

2 Answers2

5

%items is a hash of hash references (conceptually, a hash of hashes). $set is the key into %items and then you get back another hash, which is being added to with keys 'a-logdate' and 'a-dataset'.

(corrected based on comments)

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • thank you for your reply. When you say "then you get back another hash" with 2 new keys...then is it referring back to itself? – jdamae Aug 03 '10 at 18:30
  • No, conceptually each hash key-value pair in items is a key to another hash. $set is a key into %items. You get back another hash for each key. It's like a two-level tree. $set is the first level, then a-logdate is the second level. – Lou Franco Aug 03 '10 at 19:01
2

Lou Franco's answer is close, with one minor typographical error—the hash of hash references is %items, not $items. It is referred to as $items{key} when you are retrieving a value from %items because the value you are retrieving is a scalar (in this case, a hash reference), but $items would be a different variable.

Ryan M
  • 18,333
  • 31
  • 67
  • 74