I'm trying to find all array elements starting with S and N, but the below code doesn't do it:
@a = qw(sonata Samsung sony icic huawie nissan nokia);
here is my code
@filter = grep { (/^s (.*)$/ig) && (/^n(.*)$/ig) } @a;
print "@filter \n";
You're on the right track, but you've introduced whitespace into your regexes. You are also asking for cases where the string begins with the character 'S' or the character 'N', so your logic is an OR statement, rather than an AND statement as written. If all you care about is the first character, that's all you need in your pattern, and you want to use a logical OR.
@a = qw(sonata Samsung sony icic huawie nissan nokia);
@filter = grep { /^s/i || /^n/i } @a;
print "@filter \n";
Result:
sonata Samsung sony nissan nokia
Edit -- or more succinctly with a character class
@a = qw(sonata Samsung sony icic huawie nissan nokia);
@filter = grep { /^[sn]/i } @a;
print "@filter \n";