input file: mylist_1 sublist_153 sublist_87 sublist_876 sublist_78 mylist_6 sublist_8 mylist_2 sublist_12 sublist_34 sublist_09 mylist_3 sublist_87 sublist_09 mylist_7 sublist_8 sublist_9 mylist_9 sublist_56 in the above example, line 2 is a substring of line5. since line5 is longer than line2, only only line5 is taken for results. another example is line4 is a substring of line3, since line3 is longer, i take only that for results. another example: apple orange cake juice apple fruits car van bus jeep sumo hat people van car in the above example, apple in found in 2 lines, but i keep only the line which has many elements compared the other. car is found in last line and 3rd line. but i take only the 3rd line as hit because it has many elements compared to last line. so my result would be: apple orange cake juice car van bus jeep sumo hat my program: #!/usr/bin/perl open(FH,"input_file.txt") or die "can not open input file"; while($line=){ @collect=split(/\s+/,$line); push(@aoa,join("#",@collect)); } my %h; for(@aoa){ push(@uaoa,$_ if !$h{join $;, @$_}++; } foreach(@uaoa){ print "$_\n"; } the desired output for this problem: mylist_1 sublist_153 sublist_87 sublist_876 sublist_78 mylist_7 sublist_8 sublist_9 mylist_2 sublist_12 sublist_34 sublist_09 mylist_9 sublist_56