A14
A14

Reputation: 71

Perl index function to extract a certain substring

Given a certain sequence A stored in an array, I have to find if a larger sequence B contains sequence A.

I am stuck at the index part... and i'm getting an error that argument "TGACCA" isn't numeric in array element in line 69 which is:

if (index($record_r1[1], $r2_seq[$check]) != -1)


The code is:

foreach my $check (@r2_seq)
{
  if (index($record_r1[1], $r2_seq[$check]) != -1)
  {
     $matches= $matches + 1;
     print "Matched";
  }
  else
  {
  }
}

Upvotes: 0

Views: 365

Answers (2)

Ωmega
Ωmega

Reputation: 43683

I believe you wanted $check to be index, so then use the following code:

foreach my $index (0..$#r2_seq)
{
  if (index($record_r1[1], $r2_seq[$index]) != -1)
  {
     $matches= $matches + 1;
     print "Matched";
  }
  else
  {
  }
}

Upvotes: 0

ephemient
ephemient

Reputation: 204886

foreach my $check (@r2_seq)

$check takes on the value of each element in @r2_seq. It is not the index.

$r2_seq[$check]

This is attempting to use an element of @r2_seq as the index into @r2_seq. It is unlikely what you want. More probably, you want to use

$check

as in

if (index($record_r1[1], $check) != -1)

.

Upvotes: 3

Related Questions