1

I have an array in the below format

array

Link-IF-A<->IF-B
Link-IF-C<->IF-D
Link-IF-E<->IF-F
Link-IF-G<->IF-H
Link-IF-I<->IF-J

I am trying to search interface "IF-D" but the value always show as 0. I want to see 1 when it matches else 0.I ahve tried all the below method but everytime result is 0.

$link = IF-D

method1 :

my $result = grep /$link/,@array;

method 2:

my $result = grep /^$link,/,@array;

method3 :

my $result = grep(/^$link$/, @array)

Thanks

1

2 Answers 2

3

Your second and third methods can never match, as none of your target strings begin with, or contain only IF-D. The second method uses ^, anchoring to the beginning of your target string, and the third method contains both ^ and $ mandating that the pattern match the entire target string, not just some portion of it. So those will always fail (it appears that you're just trying things at random to see if they work, and especially in the case of regular expressions, that's not a good way to accomplish the goal.)

The first example will match one time; the 2nd element, because the pattern IF-D matches at the end of the target string Link-IF-C<->IF-D. However, it's only going to work if your target string and your pattern are what you think they are. In the example code you showed us, the pattern string wasn't wrapped in quotes. It must be.

So, for example, this will do what you seem to want:

my $link = "IF-D";

my @array = qw(
    Link-IF-A<->IF-B                                                            
    Link-IF-C<->IF-D                                                            
    Link-IF-E<->IF-F                                                            
    Link-IF-G<->IF-H                                                            
    Link-IF-I<->IF-J                                                            
);        

my $found = grep /\Q$link/, @array;
print "$found\n"; # 1

The \Q isn't strictly necessary for the pattern you've demonstrated. That construct forces the contents of $link to be treated on its literal meaning, rather than possibly as metasymbols. Your example pattern doesn't contain any metasymbols, but if it accidentally did, the \Q would de-meta them.

If you think you've implemented something semantically equal to this example, and yet it's not working, then you've found exactly why people ask that those asking questions post a small self-contained snippet of code that demonstrates the behavior they're describing. If my example code doesn't clear up the problem, boil your code down to a single snippet that demonstrates the problem, and add it as an update to your question so that we can run it ourselves and see exactly what you're talking about.

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

Comments

1

You must use double quote or single quote in your assignment:

$link = "IF-D";

instead of $link = IF-D.

use strict;                                                                     
use warnings;                                                                   

my @array = qw(                                                                 
    Link-IF-A<->IF-B                                                            
    Link-IF-C<->IF-D                                                            
    Link-IF-E<->IF-F                                                            
    Link-IF-G<->IF-H                                                            
    Link-IF-I<->IF-J                                                            
);                                                                              

my $link = "IF-D";                                                              

print scalar grep /$link/, @array;

8 Comments

Thanks for the response... Any other way as I have tried by putting double quotes as well.
You mean you have used double quote and the result of grep still 0?
Please explain the solution you found, as Stack Overflow is primarily about providing answers for others who have a similar problem
"You must use double quote" - that's not strictly true, is it? You need to quote the string, but you don't need to use double quotes. In fact, as this string contains no variables that you want expanded and no special escape sequences (\n, \t, etc.) I'd recommend using single quotes here.
Gnouc: False. "You must use quotes in your assignment, and those quotes may be double, or may be single. The decision should be based on whether interpolation is desired." would be correct. To say "You must use double quotes" incorrectly constrains one from using single quotes, which actually may be preferable here. It's like seeing someone eating ice cream from their hand and saying you must eat ice cream from a cone, ignoring the fact that eating it from a bowl is also just fine, and possibly better in some cases.
|

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.