0

To get the index of elements in fields containing /, I use the following but get an error msg:

@ind = grep($fields[$_] =~ /\/\/]);

Search pattern not terminated

1
  • 3
    Also, grep should have two parameters, not one: what list are you grepping? Commented Nov 8, 2013 at 10:05

2 Answers 2

5

For the sake of completeness, here's how it should look like (if array in question is @fields variable):

my @slash_indexes = grep { $fields[$_] =~ m~/~ } 0..$#fields;

Demo. The code given in the question misses the second param of grep - the list that should be grepped. Considering you need to collect indexes, you need to pass indexes of the original array into grep as well, and not the array itself.

Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't work with any of them Not enough arguments for grep at file.pl line 19, near "/\//)" Execution of file.pl aborted due to compilation errors. Not enough arguments for grep at file.pl line 19, near "m#/#)" Execution of file.pl aborted due to compilation errors.
@carol Have you checked the demo, how it's different from your code? Or have you somehow commented on another answer within this one's thread - without actually checking it? )
this one seems to work but as it was posted even with removing \, didn't @ind = grep{$fields[$_] =~ /\//} 0..$#fields;
how can I exclude the 0/0 expressions in the same line,i.e taking all expressions containing / except 0/0 @ind = grep{$fields[$_] =~ /\//} 0..$#fields; and test in another line if (grep($colNames[$curr_ind] !~ /0\/0/)){ print "$colNames[$curr_ind]\t"; } which doesn't work
You can combine two expressions in the same block: grep { $fields[$_] =~ m~/~ && $fields[$_] ne '0/0' }.
|
1

You are escaping the / that should terminate your regular expression.

Just remove the last \ from before it.

1 Comment

That's why it's often advantageous to choose another delimiter: =~ m#/# is more readable than =~ /\//.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.