Perl FIRSTKEY和NEXTKEY如何工作?

时间:2010-06-09 23:33:43

标签: perl

Tie :: Hash有这些:

sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
sub NEXTKEY  { each %{$_[0]} }

NEXTKEY接受两个参数,其中一个是最后一个键,但arg从未被引用过?

除了perltie之外,各种各样的领带文件并没有说明这一点:

my $a = keys %{$self->{LIST}};      # reset each() iterator

查看每个文档并不会增加这一点。

发生了什么事?

2 个答案:

答案 0 :(得分:11)

如果你关心最后访问了哪个密钥,你只需要担心NEXTKEY的第二个参数。默认情况下,哈希不关心顺序,因此不使用它。

对于第二部分,标量上下文中的keys函数返回散列中的项数。对键的任何调用都会重置keyseach使用的迭代器,因为它耗尽了迭代器。

拨打keys的电话实际上是对FIRSTKEY的来电,并调用NEXTKEY,直到没有剩余的项目尚未返回。

拨打each的来电是FIRSTKEY(如果尚未调用FIRSTKEY)或致电NEXTKEY(如果FIRSTKEY被称为。)

#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;
tie my %h, "HASH::Sorted", map { $_ => $i++ } "a" .. "g";

for my $key (keys %h) {
    print "$key => $h{$key}\n";
}
print "\n";

my $first = each %h;
print "first $first => $h{$first}\n";

my ($second_key, $second_value) = each %h;
print "second $second_key => $second_value\n";

print "\nall of them again:\n";
for my $key (keys %h) {
    print "$key => $h{$key}\n";
}

package HASH::Sorted;

sub TIEHASH {
    my $class = shift;

    return bless { _hash => { @_ } }, $class;
}

sub FETCH {
    my ($self, $key) = @_;

    return $self->{_hash}{$key};
}

sub STORE {
    my ($self, $key, $value) = @_;

    return $self->{_hash}{$key} = $value;
}

sub DELETE {
    my ($self, $key) = @_;

    return delete $self->{_hash}{$key};
}

sub CLEAR {
    my $self = shift;

    %{$self->{_hash}} = ();
}

sub EXISTS {
    my ($self, $key) = @_;

    return exists $self->{_hash}{$key};
}

sub FIRSTKEY {
    my $self = shift;

    #build iterator     
    $self->{_list} = [ sort keys %{$self->{_hash}} ];    

    return $self->NEXTKEY;
}

sub NEXTKEY {
    my $self = shift;

    return shift @{$self->{_list}};
}

sub SCALAR {
    my $self = shift;
    return scalar %{$self->{_hash}};
}

答案 1 :(得分:1)

这个使用自定义每个方法允许您多次迭代已排序的哈希。不允许添加或删除密钥的所有标准规则仍然有效。在调用STOREDELETE时,添加警告表示迭代器仍在使用中,这将是微不足道的。

#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;
tie my %h, "HASH::Sorted", map { $_ => $i++ } "a" .. "g";

for my $key (keys %h) {
    print "$key => $h{$key}\n";
}
print "\n";

my $first = each %h;
print "first $first => $h{$first}\n";

my ($second_key, $second_value) = each %h;
print "second $second_key => $second_value\n";

print "\nall of them again:\n";
for my $key (keys %h) {
    print "$key => $h{$key}\n";
}

print "\nmultiple iterators\n";

my $o = tied %h;
while (my ($k, $v) = $o->each("outer")) {
    print "$k => $v\n";

    while (my ($k, $v) = $o->each("inner")) {
        print "\t$k => $v\n";
    }
}

print "\nhybrid solution\n";
while (my ($k, $v) = each %h) {
    print "$k => $v\n";

    #the iter_name is an empty string
    while (my ($k, $v) = $o->each) {
        print "\t$k => $v\n";
    }
}


package HASH::Sorted;

sub each {
    my ($self, $iter_name) = (@_, "DEFAULT");

    #each has not been called yet for this iter
    unless (exists $self->{_iters}{$iter_name}) {
        $self->{_iters}{$iter_name} = [ sort keys %{$self->{_hash}} ];
    }

    #end of list
    unless (@{$self->{_iters}{$iter_name}}) {
        delete $self->{_iters}{$iter_name};
        return;
    }

    my $key = shift @{$self->{_iters}{$iter_name}};

    if (wantarray) {
        return $key, $self->{_hash}{$key};
    }

    return $key;
}

sub TIEHASH {
    my $class = shift;

    return bless {
        _hash => { @_ },
        _iters => {},
    }, $class;
}

sub FETCH {
    my ($self, $key) = @_;

    return $self->{_hash}{$key};
}

sub STORE {
    my ($self, $key, $value) = @_;

    return $self->{_hash}{$key} = $value;
}

sub DELETE {
    my ($self, $key) = @_;

    return delete $self->{_hash}{$key};
}

sub CLEAR {
    my $self = shift;

    %{$self->{_hash}} = ();
}

sub EXISTS {
    my ($self, $key) = @_;

    return exists $self->{_hash}{$key};
}

sub FIRSTKEY {
    my $self = shift;

    #build iterator     
    $self->{_list} = [ sort keys %{$self->{_hash}} ];    

    return $self->NEXTKEY;
}

sub NEXTKEY {
    my $self = shift;

    return shift @{$self->{_list}};
}

sub SCALAR {
    my $self = shift;
    return scalar %{$self->{_hash}};
}