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.

Ganesh Sittampalam
  • 28,821
  • 4
  • 79
  • 98
Xpx
  • 137
  • 1
  • 2
  • 7

1 Answers1

3
my %seen;
@array = grep !$seen{ substr($_,0,4) }++, @array;
ysth
  • 96,171
  • 6
  • 121
  • 214
  • Thank you it works How about this @array = qw (abcd.txt abcdeff.txt abcdweff.txt abcdefrgt.txt efghijk.txt efghijksdf.txt efghijkasdfw.txt); – Xpx Apr 29 '15 at 01:05
  • 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 – ysth Apr 29 '15 at 01:17
  • Yea, it works as well. – Xpx Apr 30 '15 at 01:47