perlknight has asked for the wisdom of the Perl Monks concerning the following question:
I am having problem with hash of hash; it's two level deep. How do I handle storing the value of "self->{CURRENT}{$id}{DOWN} or self->{CURRENT}{$id}{UP}"? Thanks for your help.package Triger; use strict; use Carp; # Create tie hash sub TIEHASH { my $self = shift; my $path = shift; my $mode = shift || 'r'; if(@_) { croak("Usage: tie(\%hash, \$file, [mode])"); my($line, $id, $status, $time_down, $time_up); foreach $line (@lines) { ($id,$status) = split(/=/,$line); ($time_down,$time_up) = split(/\|/,$status); $node->{CURRENT}{$id}{DOWN} = $time_down; $node->{CURRENT}{$id}{UP} = $time_up; } return bless $node => $self; } sub FETCH { my $self = shift; my ($id) = shift; if ( exists $self->{CURRENT}{$id} ) { return $self->{CURRENT}{$id}; } else { return "$id doesn't exist"; } } sub STORE { my $self = shift; my ($id) = shift; my($TFile) = $self->{PATH}; my $time_down; my $time_up; my($return) = 0; my(@cache); unless($self->{CLOBBER}) { carp ("No write access for $self->{PATH}"); return; } # open File if(!open(FH, "<TFile")) { carp("Cannot open $TFile: $!"); return; } flock($TFile,2); if(!exists $self->{CURRENT}{$id}) { while(<FH>) { if( /^$id\=/ ) { push (@cache, "$id=$time_down|$time_up +\n"); $return = 1 } else { push (@cache, $_); } } } close(FH); if($return) { # Writing to File if (!open(FH,">$TFile")) { carp("Cannot open $TFile: $!"); return; } flock(FH, 2); while (@cache) { print FH shift (@cache); } } else { if(!open(FH,">>$TFile")) { carp("Cannot open $TFile: $!"); return; } flock(FH,2); print FH "$id=$time_down|$time_up\n"; } close(FH); }
Edit kudra, 2002-04-16 Added readmore
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Having problem with tiehash
by Juerd (Abbot) on Apr 15, 2002 at 22:14 UTC |