in reply to More efficient Data Structure needed

A lookup hash is a good idea, as they are faster than iterating through an array. Also, you can do a normal string equality test (eq) rather than compiling the regular expression and firing up the regex engine. Finally, you can use just normal string interpolation rather than join since you're doing nothing too fancy there.

It looks like @data is likewise two-dimensional, so assuming that here's something that might work for you:

my %lookup = map { $_->[0] => $_ } @m_info; # key each array by its fi +rst entry. foreach my $record (@data) { my $data = $lookup{$record->[1]}; $record->[1] = "$data->[3]_$data->[1]" if $data; }

Replies are listed 'Best First'.
Re: Re: More efficient Data Structure needed
by BrowserUk (Patriarch) on Dec 18, 2002 at 02:43 UTC

    The problem with that is that he isn't doing a one-for-one compare between the two things, he's checking to see if one contains the other (hence the regex). Which make sthe whole problem rather harder.

    if ($record->[1] =~ /$data->[0]/) {

    Examine what is said, not who speaks.

      Actually that isn't made explicitly clear in the original post, although I agree the absence of anchors certainly indicates that you're correct.

      Tie::Hash::Regex would work in that case, but unfortunately all the efficiency of the hash would be lost as that module simply iterates over the keys in the hash doing a regex match :^\

      If he only needs substring matching, then index is faster and safer than regular expressions. He wouldn't have to worry about escaping characters or compiling the regex.
      if (index($record->[1], $data->[0]) >= 0) {