0

I am new to Perl programming.

I need to find and delete partially matching strings in an array.

For example in my array there are strings:

@array = qw(abcd.txt abcdeff.txt abcdweff.txt abcdefrgt.txt);

I just want the first abcd.txt to be saved to the array and to delete the rest (which are similar in the first 4 characters) i.e. so that it will print only abcd.txt when @print "@array"; is called.

2
  • This is not a duplicate, it differs somewhat. Commented Apr 28, 2015 at 19:21
  • @print "@array"; is wrong. It should be print "@array"; Commented Apr 28, 2015 at 20:13

1 Answer 1

3
my %seen;
@array = grep !$seen{ substr($_,0,4) }++, @array;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you it works How about this @array = qw (abcd.txt abcdeff.txt abcdweff.txt abcdefrgt.txt efghijk.txt efghijksdf.txt efghijkasdfw.txt);
what about that? it should work for that too, getting rid of any elements that have the same first four characters as a previous element
Yea, it works as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.