Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Hash of hashes

by AnomalousMonk (Canon)
on Apr 06, 2013 at 00:27 UTC ( #1027234=note: print w/replies, xml ) Need Help??

in reply to Hash of hashes

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).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027234]
[Lady_Aleena]: Why does grep(/.*$in.*/, @my_modules) work but grep { $_ =~ /.*$in.*/; $_ } @my_modules not work? What did I do wrong in the second?
[tobyink]: Why are you returning $_ n your grep block? You should return a boolean.
[tobyink]: *in your grep block
[tobyink]: You can use grep { $_ =~ /.*$in.*/; } @my_modules but why not stick to grep(/.*$in.*/, @my_modules)? (The latter is faster.)
[shmem]: Lady_Aleena, in the first example grep evaluates the result from grep and if true, returns $_. In the second, it always returns $_
[shmem]: ..the result from the pattern match
[Lady_Aleena]: tobyink, I did after I failed to get the BLOCK to work. I can't seem to get my brain around grep BLOCK, though I'm okay with grep EXPR.
[shmem]: so in the second example grep returns all true elements of the list passed
[Lady_Aleena]: Okay, so grep BLOCK is not like map BLOCK where something might need to be returned at the end.
[tobyink]: grep { $_ =~ /.*$in.*/; } @my_modules should work just fine. The problem is that you were adding on ;$_ at the end of the block. Why were you doing that?

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-05-27 07:40 GMT
Find Nodes?
    Voting Booth?