in reply to "array" search

Well, that’s a very clumsy data structure you have there. And the code is clumsier than it needs to be, on top of that. What’s that count doing? Looks like a no-op to me. And don’t use for(;;), use for(@LIST):

for my $i ( 0 .. $#{ $self->{ keys } } ) { my $id = $self->{ keys }->[ $i ]; # ... }

You could write accessor methods for that part of the data structure so you could say something like:

for my $i ( 0 .. $self->max_index() ) { my ( $key, $value ) = $self->at( $i ); # ... }

Or maybe an iterator would be better?

my $iter = $self->iterator(); while( my ( $key, $value ) = $iter->() ) { # ... }

Or you could use one of the modules that implement ordered hashes, such as Tie::IxHash, which will let you write this directly using keys and friends.

Lots of options that depend on what you want to do; for laziness, the tied hash is hard to beat.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: "array" search
by Sheol (Novice) on Jan 14, 2006 at 08:28 UTC

    Hmmm... just checked count in perldoc... what I was trying to do was to compare the $i with length of the array... I could have sworn count() was the function.

    I would write acessors, but I am too lazy (and this search function appears in many objects.. not only one... thats part of the reason I figured an... inline... function (or macro) would work)

    The iterator would appear to be something I wanted to ask about in a future post. What would I have to extend (via @ISA) to make use of it?

    Though like I said, I was already considering using a Tied hash... thanks for pointing me to the first step.