You generally do that by storing all your data into a hashtable keyed on the properties you want to be unique. A hashtable stores tuples of (key,value). In this particular case the value is of no concern, only the key is important. That's why below the value is always 1. And let the hashtable do the hard work knowing the principle that hashtable's keys are unique. Here is an example:

use strict; use warnings; my %UH; $UH{'k1'} = 1; # hash contains 1 item $UH('k2'} = 1; # hash contains 2 items $UH{'k1'} = 1; # at this point the hash still contains 2 items for my $k (sort keys %UH){ print "Hash contains key '$k'\n"; }

If your data is stored in an array then you make items unique by creating a hashtable from the array (%{ {map { $_ => 1 } @items} }) and then taking the keys of that:

use strict; use warnings; my @items = ('ü','ü','ä', 'Ä'); my @unique_items = sort keys %{ {map { $_ => 1 } @items} }; print "@unique_items\n";

Note that sorting is optional of course.

Alternatively, you can use a perl module to do what you want, see e.g. here: https://perlmaven.com/unique-values-in-an-array-in-perl


In reply to Re: Counting matches and removing duplicates by bliako
in thread Counting matches and removing duplicates by LexPl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.