seems confusing know. i'll try "draw" data structure:
hash-> key->( (key)->[(key,value),(key,value),(key,value),...], (key,value))
so there first key, value enclosed in parenthesis. value first key of hash 2 keys, 1 (the right one) being simple key, value pair. other (the left one) key's value array of hashes. able update "right" key, value pair following line of code:
$hash{$parts[1]}{"pages"} += $parts[2];
where $parts[1] , $parts[2] elements array. +=ing number "right" key, value pair hash. need update "left" key,value pair - array of hashes within hash of hashes. here how initialize array both key, value pairs in hash of hashes:
$hash{$printer}{"pages"} = 0; $hash{$printer}{"users"} = [@tmp];
here 1 of many attempts access , update values in array of hashes:
$hash{$parts[1]}{"users"}[$parts[0]] += $parts[2];
i can't figure out correct syntax this. if me i'd appreciate it. thanks.
edit: guess more pointed question is: how hash key array of hashes (keeping in mind array in hash of hashes)?
edit: added code:
#go through each user check see user did print job , add page #count total #$parts[0] user name, $parts[1] printer name, $parts[2] page #count current print job for(my $i=0;$i<$arr_size;$i++) { $inner = $hash{$parts[1]}{"users"}[$i]; @hash_arr = keys %$inner; $key = $hash_arr[0]; #problem line - need compare actual key $parts[0] #(not key's value number) if($hash{$parts[1]}{"users"}[$i]{$key} eq $parts[0]) { $hash{$parts[1]}{"users"}[$i]{$parts[0]} += $parts[2]; } }
edit: whoops hehe needed. still isn't quite there kind of looking for:
if($key eq $parts[0]) { $hash{$parts[1]}{"users"}[$i]{$parts[0]} += $parts[2]; }
edited respond edited question: how hash key array of hashes (keeping in mind array in hash of hashes).
use strict; use warnings; %h; $h{printer}{pages} = 0; $h{printer}{users} = [ {a => 1, b => 2}, {c => 3, d => 4}, {e => 5, f => 6}, ]; # access particular element. $h{printer}{users}[0]{a} += 100; # access 1 of inner hashes. $inner = $h{printer}{users}[1]; $inner->{$_} += 1000 keys %$inner; # ditto, without convenience variable. $h{printer}{users}[2]{$_} += 9000 keys %{ $h{printer}{users}[2] }; use data::dumper qw(dumper); print dumper \%h;
output:
$var1 = { 'printer' => { 'pages' => 0, 'users' => [ { 'a' => 101, 'b' => 2 }, { 'c' => 1003, 'd' => 1004 }, { 'e' => 9005, 'f' => 9006 } ] } };
Comments
Post a Comment