develooper Front page | perl.perl6.internals | Postings from August 2002

[prepatch] PerlHash iterator

Thread Next
From:
Aldo Calpini
Date:
August 22, 2002 07:40
Subject:
[prepatch] PerlHash iterator
Message ID:
157209315063.20020822163802@alos.it

I have implemented a simple hash iterator, but I'm not
sure that the logic is correct. this shows how the PMC
behaves actually:

        new P1, .PerlHash   # build a simple hash
        set P1["foo"], 1
        set P1["bar"], 2
        set P1["baz"], 3

        dec P1              # reset the iterator
LOOP:
        unless P1, END      # when false, iterator has ended
                            # (eg. there's no current key)
        set S0, P1          # fetch the current key
        print S0            # print it
        set I0, P1[S0]      # fetch the current value
        print "="
        print I0            # print it
        print "\n"
        inc P1              # iterate to next key
        branch LOOP
END:
        end

and the output is:
baz=3
foo=1
bar=2

I have implemented the following functions in hash.c:

    void hash_reset(Interp *interpreter, HASH *hash);
    void hash_next(Interp *interpreter, HASH *hash)
    STRING *hash_get_current(Interp *interpreter, HASH *hash)

....and the following PMC vtable methods:

    STRING* get_string () {
        STRING* s;
        s = hash_get_current(INTERP, (HASH*) SELF->data);
        if(s == NULL)
            return (STRING*) undef->vtable->get_string(INTERP, undef);
        else
            return s;
    }

    void decrement () {
        hash_reset(INTERP, (HASH*) SELF->data);
    }

    void increment () {
        hash_next(INTERP, (HASH*) SELF->data);
    }

    INTVAL get_bool () {
        if(hash_get_current(INTERP, (HASH*) SELF->data) == NULL)
            return 0;
        else
            return 1;
    }

what do you think about it? it is worthy a patch, or should I
change my approach?

cheers,
Aldo

__END__
$_=q,just perl,,s, , another ,,s,$, hacker,,print;


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About