Tuesday, 15 September 2015

perl - Hash value is not re-initialized when loop is terminated with 'last' keyword -



perl - Hash value is not re-initialized when loop is terminated with 'last' keyword -

consider next nested loops:

my %deleted_documents_names = map { $_ => 1 } $self->{manual}->get_deleted_documents(); while($sth->fetch){ ..... ..... ..... while(my ($key, $value) = each(%deleted_documents_names)){ { if($document_name eq $key){ $del_status=1; last; } } if($del_status==1){next;} ..... ..... ..... ..... }

now, take sample case 3 values (a,b,c) compared against 2 values (b,c).

first scan: compared b compared c sec scan: b compared b loop terminated. 3rd scan: c compared c.

in case, c should compared first b, beingness first value, comparing skipped, , scans next element after 1 found equal. if remove lastly termination status , allow loop run total number of scans, works fine, need find out why in case, $key refers next compared value , not first value 1 time loop restarted after getting terminated lastly keyword.

any help appreciated.

use

keys %deleted_documents_names ; # reset "each" iterator.

see keys.

but, why iterating on hash? why don't just

if (exists $deleted_documents_names{$document_name}) {

perl perl-data-structures

No comments:

Post a Comment