in reply to retrieving in the correct order

I take it the first array is relatively small? If so, you can just turn your loops inside out:

for my $i (@array2) { for my $line (@array1) { my $key = "gi|$i|"; if (substr($line, 0, length $key) eq $key) { print $line; } } }
Notice how you don't even need a regex, just a simple string compare.

If array1 is sorted, you can speed this up a little by remembering where you left off.

Replies are listed 'Best First'.
Re^2: retrieving in the correct order
by Anonymous Monk on Dec 16, 2004 at 20:21 UTC
    Hi VSarkiss, Thanks for your solution but I can't get it to work! Maybe it's becuase my first array is quite big (~1000 sequences). But wouldn't this just slow it down? Thanks

      Well, some detail on what went wrong would help....

      When I tried it against the sample data in your original post, I noticed two things:

      1. You're testing for gi| at the beginning of the line, but your @array1 values start with >gi|. I had to remove the >; you'll have to either fix the $key = line to match your data, or fix the data to match your test...
      2. You're populating a single element in array2. If you want each number to be an element of the array, you need to use qw(...), not qq(...).

      If these are both copy-and-paste artifacts, pleave provide more detail on what the error is.

        Sorry,

        I should have been more specific.

        The two problems that you noticed are not problems in my program, just in the example I gave in my question.

        With the code that you posted I don't get any error - but I don't get any output either ;-( The $key is definately picking up values (e.g. gi|14028145 |) so I think the problem is with substr. Also, the values of @array2 are present in @array1 as I have checked.

        Hope you can help.

        Thanks!

      hey, check my code below, i tested it with a file with 3000 lines in it. time cat gen.txt | perl -w gen.pl says:
      real 0m0.139s user 0m0.109s sys 0m0.000s

      if you're still looking for an answer...

      --
      to ask a question is a moment of shame
      to remain ignorant is a lifelong shame