Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Hash of hashes

by gibsonca (Beadle)
on Apr 05, 2013 at 22:13 UTC ( #1027227=perlquestion: print w/replies, xml ) Need Help??
gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for your time. I'm stuck on getting this data into some type of hash of hashes, or array of hashes. My efforts have been so weak, I can not put them here. This is what the data looks like:

tt.exe Quotas start finish ABC 10 4 def 3 2 ggg 1 0 ww.exe Quotas start finish ABC 430 5 def 33 10 ggg 10 4 xx.exe Quotas start finish ABC 60 22 def 23 3 ggg 11 10

So I wanted the key to the hash to be the executable name, and the data to follow. I should ignore the line with Quotas start finish, that can be presumed. I have looked at many hash examples, and for some reason I am not getting it.

Replies are listed 'Best First'.
Re: Hash of hashes
by choroba (Bishop) on Apr 06, 2013 at 00:10 UTC
    If your question was how to read the data into the hash, here is one possible way:
    #!/usr/bin/perl use warnings; use strict; my $exe; my %hash; while (<>) { my ($quotas, $start, $finish) = split ' '; if (not defined $finish) { $exe = $quotas; <>; # Skip the following header. } else { $hash{$exe}{$quotas} = [ $start, $finish ]; } } print $hash{'ww.exe'}{def}[0];
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Hash of hashes
by AnomalousMonk (Chancellor) on Apr 06, 2013 at 00:27 UTC

    It looks like you might want to wind up with a program having a hash-of-hashes for which the following statements were true (given the example data):
        my %my_data;
        if ($my_data{'tt.exe'}{'ABC'}{'start'} == 10) { print qq{it's true! \n}; }
        if ($my_data{'xx.exe'}{'def'}{'finish'} == 3) { print qq{also true! \n}; }

    How would one produce a hash having an element for which the expression
        $my_data{'tt.exe'}{'ABC'}{'start'} == 10
    was true? The simple statement
        $my_data{'tt.exe'}{'ABC'}{'start'} = 10;
    does the trick (note = assignment in place of the == numeric equality operator).

    A more generic program fragment might look something like
        my %my_data;
        my $record = get_record_from_raw_data();
        my $name   = get_executable_name_from($record);
        my $quota  = get_quota_from($record);
        my $start  = get_start_from($record);
        my $finish = get_finish_from($record);
        $my_data{$name}{$quota}{'start'}  = $start;
        $my_data{$name}{$quota}{'finish'} = $finish;

    How would one extract a record from your data? That's very dependent on the exact definition of the structure of your data, which you don't give. However, your example suggests that your data is a set of multi-line records and that the executable-name field of each record is always the first field (i.e., first line) in the record, and real data in this field always starts at the very first column of the line (i.e., the line has no leading whitespace); this could be used to recognize the beginning of a record if the data file were processed line-by-line rather than by extracting each record in one gulp as implied in the generic pseudo-code above. All subsequent fields seem to begin with some kind of leading whitespace, and the first field (i.e., the first line) after the executable-name field, the redundant quota-start-finish header name field, can always be ignored.

    If the data file were processed on a line-by-line basis, a regex could be used to recognize the beginning field of each record and extract the executable name from the field, and split could be used on each subsequent field of the record (after skipping the redundant field) to extract the quota-start-finish sub-field data from each field. (Of course, all this ignores important data validation considerations.)

    And then you do something with all your extracted data, and there's your program!

    See also the Perl Data Structures Cookbook (perldoc perldsc).

Re: Hash of hashes
by Loops (Curate) on Apr 05, 2013 at 22:34 UTC

    Here's one way. A hash entry for each executable referencing a hash that contains the lettered keys which in turn reference an array of two elements, "start" and "stop:

    my %data = ( 'tt.exe' => { ABC => [ 10, 4 ], def => [ 3, 2], ggg => [ 1 , 0], }, 'ww.exe' => { ABC => [ 430, 5], def => [ 33 , 10], ggg => [ 10 , 4], }, 'xx.exe' => { ABC => [ 60 , 22], def => [ 23 , 3], ggg => [ 11 , 10], }, ); $,=' '; print "start and finish :", @{$data{'ww.exe'}->{def}}, "\n";
Re: Hash of hashes
by Anonymous Monk on Apr 06, 2013 at 07:32 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027227]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-05-25 03:48 GMT
Find Nodes?
    Voting Booth?