[LON-CAPA-cvs] cvs: newloncapa /types HashIterator.pm
foxr
lon-capa-cvs@mail.lon-capa.org
Thu, 10 Apr 2003 01:56:23 -0000
foxr Wed Apr 9 21:56:23 2003 EDT
Modified files:
/newloncapa/types HashIterator.pm
Log:
Debugged version of HashIterator.
Index: newloncapa/types/HashIterator.pm
diff -u newloncapa/types/HashIterator.pm:1.1 newloncapa/types/HashIterator.pm:1.2
--- newloncapa/types/HashIterator.pm:1.1 Wed Apr 9 15:41:45 2003
+++ newloncapa/types/HashIterator.pm Wed Apr 9 21:56:23 2003
@@ -37,30 +37,36 @@
=pod
=head2 new(hash)
+
Create a new HashIterator object and return a reference to it.
Data members of the HashIterator include:
=item Hash
+
Reference to the hash being iterated over.
=item Keylist
+
The set of keys in the underlying hash (an anonymous array ref).
=item KeyCount
+
The number of keys in the underlying hash.
=item Index
+
Position of the iterator within the keylist/hash table.
=cut
sub new {
my $class = shift; # Class name...
my $hashref = shift; # Maintain this hash.
- my $self = []; # We are an anonymous hash.
+ my %hash = %{$hashref};
+ my @keylist = keys(%hash);
+ my $keyref= \@keylist;
+ my $keycount = scalar @keylist;
- my @keys = keys %$hashref;
- my $keyref= \@keys;
- $self = { Hash => $hashref,
- Keylist => $keyref,
- KeyCount => scalar @keys,
- Index => 0 };
+ my $self = { Hash => $hashref,
+ Keylist => $keyref,
+ KeyCount => $keycount,
+ Index => 0};
bless($self, $class); # Type ourself...
return $self;
@@ -74,6 +80,7 @@
sub begin {
my $self = shift; # Get object...
$self->{Index} = 0;
+
}
=pod
@@ -100,7 +107,6 @@
my $keysref = $self->{Keylist};
my @keys = @$keysref;
my $key = $keys[$self->{Index}];
-
return $$hashref{$key};
}
=pod
@@ -109,7 +115,7 @@
=cut
sub next {
my $self = shift; # Get us.
- $self->{Index} = $self->{Index}++;
+ $self->{Index} = $self->{Index} + 1;
}
1;