in reply to How can I make a string unique (quicker than my approach at least)

This solution preserves order, if that is an issue.

johngg@aleatico:~/perl/Monks$ perl -Mstrict -Mwarnings -E 'say q{}; open my $inFH, q{<}, \ <<__EOD__ or die $!; ID1<TAB>nick-john-helena ID2<TAB>george-andreas-lisa-anna-matthew-andreas-lisa ID3<TAB>olivia-niels-peter-lars-niels-lars-olivia-olivia __EOD__ while ( <$inFH> ) { chomp; my( $pre, $post ) = split m{(?<=>)}; say $pre, join q{-}, do { my %seen; grep { ! $seen{ $_ } ++ } split m{\s?-\s?}, $pos +t } }' ID1<TAB>nick-john-helena ID2<TAB>george-andreas-lisa-anna-matthew ID3<TAB>olivia-niels-peter-lars

I hope this is of interest.

Cheers,

JohnGG

  • Comment on Re: How can I make a string unique (quicker than my approach at least)
  • Download Code

Replies are listed 'Best First'.
Re^2: How can I make a string unique (quicker than my approach at least)
by hippo (Archbishop) on Apr 02, 2024 at 11:12 UTC
    my( $pre, $post ) = split m{(?<=>)};

    Interesting - you have assumed that the <TAB> in the OP's data is literally those 5 characters whereas in my reading they were using this to indicate a single tab character. Doesn't matter really, but it would make the split regex simpler if it were a single tab.


    🦛