rlmoe has asked for the wisdom of the Perl Monks concerning the following question:
The following code is providing inconsistent output. After the code is some output from runs: the first run with 200 records yields what I expect; the second, with 250 records, yields something I cant explain. Is it not permitted to change the value of a tied hash in an each iteration?
use strict; print "$^V\n"; my $records=shift; unlink "test_tie_hash"; use BerkeleyDB; my %MAIN; tie %MAIN, "BerkeleyDB::Hash", -Filename => "test_tie_hash", -Flags => + DB_CREATE, or die "Cannot open file: $! $BerkeleyDB::Error\n"; my $key="AAAAAAA"; my $store="LLLL"; foreach (1 .. $records){ ++$key; $MAIN{$key}= $store; } untie %MAIN; tie %MAIN, "BerkeleyDB::Hash", -Filename => "test_tie_hash", -Flags => + DB_CREATE; my %count=(); while ((my $key,my $value)=each (%MAIN)){ $count{$value}++; } untie(%MAIN); foreach(keys(%count)){ print "INITIAL LOAD\n$_ $count{$_} records\n\n"; } %count=(); $key=""; tie %MAIN, "BerkeleyDB::Hash", -Filename => "test_tie_hash", -Flags => + DB_CREATE; while ((my $key,my $value)=each (%MAIN)){ if($key=~m/U/){ $value .= "-YYYYYYYYYYYYYYYYYYYYYYYYYYYY"; $MAIN{$key}=$value } elsif($key=~m/L/){ $value .= "-ZZZZZZ"; $MAIN{$key}=$value; } else{ $value .= "-WWWWWWWWWWWWWWWWW"; $MAIN{$key}=$value; } } untie(%MAIN); tie %MAIN, "BerkeleyDB::Hash", -Filename => "test_tie_hash", -Flags => + DB_CREATE; while((my $key,my $value)=each (%MAIN)){ #print "$key $value\n"; $count{$value}++; } untie(%MAIN); foreach(keys(%count)){ print "$_ $count{$_}\n"; }
OUTPUT
$ perl -w test_each_tie 200 v5.10.0 INITIAL LOAD LLLL 200 records LLLL-WWWWWWWWWWWWWWWWW 185 LLLL-YYYYYYYYYYYYYYYYYYYYYYYYYYYY 7 LLLL-ZZZZZZ 8
$ perl -w test_each_tie 250 v5.10.0 INITIAL LOAD LLLL 250 records LLLL-WWWWWWWWWWWWWWWWW 101 LLLL-YYYYYYYYYYYYYYYYYYYYYYYYYYYY-YYYYYYYYYYYYYYYYYYYYYYYYYYYY 5 LLLL-ZZZZZZ-ZZZZZZ 6 LLLL-YYYYYYYYYYYYYYYYYYYYYYYYYYYY 4 LLLL-WWWWWWWWWWWWWWWWW-WWWWWWWWWWWWWWWWW 114 LLLL 16 LLLL-ZZZZZZ 4
It seems that some values in the tied hash are unaltered and some are altered more than once. Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: altering value in tied hash
by choroba (Cardinal) on Jun 26, 2016 at 07:21 UTC | |
by Anonymous Monk on Jun 26, 2016 at 09:25 UTC | |
by rlmoe (Initiate) on Jun 26, 2016 at 15:23 UTC | |
by Anonymous Monk on Jun 26, 2016 at 21:11 UTC | |
by rlmoe (Initiate) on Jul 01, 2016 at 03:27 UTC | |
|
Re: altering value in tied hash
by Anonymous Monk on Jun 26, 2016 at 10:21 UTC | |
by Anonymous Monk on Jun 26, 2016 at 10:26 UTC |