Re: Is it possible to evaluate a changing hash (changed within a loop)?

zhilianghu@xxxxxxxxx wrote:
> while (($name, $count) = each %examiner ) {

Showing code is good. Thank you. Unfortunatley, you didn't show enough
of it.

> # - find parent of $name

Say it in perl.
my $parent = find_parent($name);

> # - see of the parent qualify

if (is_qualifying($parent)) {

> # - if yes, update the %examiner to turn the "count" to "0";

$examiner{$name}=0; # is this what you meant?

> # - if not, add new parent(s) to %examiner for next round evaluation

} else {
$examiner{$parent}=1; # Is this what you mean?

> # - if all elements in the hash are "0", finish.
last unless grep $_>0 , values %examiner.
> }

Ignoring your actual question for minute, I don't see how this code
(assuming my guesses were right) will ever finish. If a name does
not have a direct parent which qualifies, then its parent will get added to
the hash, but the child itself will never get set to zero. Also, once a
entry is set to zero, it never has to be looked up again, right?

> My question is, can a hash be updated while being evaluated? In other
> words, can a hash be "re-evaluated" until its changing value satisfy
> certain criteria?

perldoc -f each

If you add or delete elements of a hash while you're
iterating over it, you may get entries skipped or
duplicated, so don't.

> So far, my experimenting shows it only evaluates each element once,
> then quit, regardless how the hash content is changed. Is another loop
> structure needed in order to loop for the 2nd, 3rd,... rounds
> evaluation (on the updated hash)? Or there is a simpler way...?

There are many ways. Here is one (of course, it may be an infinite
loop, as discussed above. I can't solve that part, as I don't know what
you really want.

while (my @x = grep $examiner{$_}>0 , keys %examiner) {
foreach my $name (@x) {
my $parent = find_parent($name);
if (is_qualifying($parent)) {
} else {
$examiner{$parent}=1 unless exists $examiner{$parent};

This is kind of like breadth first search. I would probably actually
do it as a depth first search, but that doesn't seem to fit in with
sample code you provided. I'm not entirely sure how this solves your
prose problem description, as you only describe count as getting
decremented, never incremented, so I don't know how anything is supposed to
actually get counted.


-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB