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.


In reply to altering value in tied hash by rlmoe

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.