0

How to grep only multiple sets of five characters in any order in an array in a Perl program?

@arr2 = grep(/[BCRZ]+/, @arr1);

@arr1 can contain

CRZBBZTCCBBRZ
FJDLSFJSLFJS
CRZBBZCCBBRZ

only the lines like the last should be taken

2
  • I don't understand why the first line should be excluded. And where are your five characters? Do you mean the string has to contain at least five of BCRZ? Commented May 16, 2014 at 14:31
  • I missed the 5th letter. Let's take some thing different from T like W. In this case, the 1st and 2nd should be discarded. Any set of 5, CBRZW in any order in any length is accepted Commented May 16, 2014 at 14:35

2 Answers 2

3

If what is you want is lines that only contain the 5 chars and none others, then a regex like:

 /^[BRCZW]+$/

looks for strings containing one or more of your 5-character set, but containing no other characters. But it might be more efficient to us @carol's solution using grep(). Which uses a regex to determine if the string has any of the unwanted characters, and then rejects that line.

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

2 Comments

As I suggested in the comments below @Borodin's answer, this could be used in exactly the same way inside a grep. How were you suggesting it could be used instead?
I don't think your answer was in my feed when I typed mine. But yes, I am suggesting the same thing as you.
3

I think this may do what you want. It rejects a string if it contains any character other than CBRZW.

use strict;
use warnings;

my @arr1 = qw/ CRZBBZTCCBBRZ FJDLSFJSLFJS CRZBBZCCBBRZ /;

my @arr2 = grep { not /[^CBRZW]/ } @arr1;

print "$_\n" for @arr2;

output

CRZBBZCCBBRZ

3 Comments

but it seems that with "not", it holds any thing except BCRZ, no? which is the opposite of what I want
@carol this is a double negative - there's the not and the ^ in the character class. Another way you could do this is with grep { /^[BCRZ]+$/ } @arr1; - one or more characters from that class between the start and end of the string.
@carol: Tom is correct. As I wrote, the code rejects a string if it contains any character other than CBRZW. Also, the code produces the output that you requested.

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.