CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
hash and dbm

 



pauls
Novice

Mar 31, 2001, 9:19 PM

Post #1 of 3 (108 views)
hash and dbm Can't Post

I have a hash that is tied to a dbm file within a sub. All works great except, the key & value of the last run of the sub is the only thing written to the file.
It seems to be over writing the file??
So I guess what I'm asking, is how can I run the same hash routine multi. times and have it write the new info to the file.
Here is the sub.

sub units {
$units4x5 = "units4x5$imagenum"; # where $imagenum = 0001 thru 0500
$units5x7 = "units5x7$imagenum"; # dependent upon earlier user selection.
$units8x10 = "units8x10$imagenum";
$units11x14 = "units11x14$imagenum";
$unitstotal = "unitstotal$imagenum";
#$filename is a user unique value. Ie. username
dbmopen(%units, "units$filename", 0666) || die "can't open units$filename dbm: $!";
%units=($units4x5 => $picunits4x5, # $picunits*x* = a numeric value.
$units5x7 => $picunits5x7,
$units8x10 => $picunits8x10,
$units11x14 => $picunits11x14,
$unitstotal => $picunitstotal,
);
dbmclose(%units);
}

In a different file is use the following to display the info from the file.
Like this which by the way is also in a sub.

dbmopen(%unit, "units$filename", 0666) || die "can't open units$filename dbm: $!";
while( ($key, $value)=each %unit) {
print " $key = $value\n";
}
closedbm(%unit);

which will display this:
units4x50002 = 1 units5x70002 = 1.6 units8x100002 = 2 units11x140002 = 2.4 unitstotal0002 = 7

pls excuse the sloppy writing I'm still learning.....
thanks for any help
paul




pauls
Novice

Apr 3, 2001, 8:00 AM

Post #2 of 3 (98 views)
Re: hash and dbm [In reply to] Can't Post

I'll assume that no responses means it can't happen......

paul



japhy
Enthusiast

Apr 3, 2001, 9:24 AM

Post #3 of 3 (97 views)
Re: hash and dbm [In reply to] Can't Post

The problem is you're SETTING the hash:


Code
%units = ( 
$units4x5 => $picunits4x5, # $picunits*x* = a numeric value.
$units5x7 => $picunits5x7,
$units8x10 => $picunits8x10,
$units11x14 => $picunits11x14,
$unitstotal => $picunitstotal,
);

You probably just want to set those keys:


Code
@units{ 
$units4x5, $units5x7, $units8x10, $units11x14, $unitstotal
} = (
$picunits4x5, $picunits5x7, $picunits8x10, $picunits11x14, $picunitstotal
);

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives