Set 1 (@data): g123|10249875 g948|32004040 Set 2 (@ref): g123:1|10249870 g123:2|10249875 g123:3|10249871 g984:2|94950595 g984:1|32004040 #### foreach $elem(@data){ ($sub,$max)=split/\|/,$elem; @found=grep /^$sub:/,@ref; foreach $f(@found){ ($id,$time)=split/\|/,$f; if($time ne $max){ push @set,$id; } } } #### @set: g123:1 g123:3 g984:2