All, this is my first attempt in using tiehash. I have a file which look like "hostname=time_system_down|time_system_up" and I want to use tiehash on it. I am able to fetch the value but not store it. Here's the code:
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); }
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.

Edit kudra, 2002-04-16 Added readmore


In reply to Having problem with tiehash by perlknight

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.