set in Perl?

Discussion in 'Perl Misc' started by Davy, Aug 30, 2006.

  1. Davy

    Davy Guest

    Hi all,

    I used to use set (only contain one identical thing in one set) in
    C++'s STL. I found set very useful that you can do something like
    intersection. Is there something similar in Perl? Thanks!

    Best regards,
    Dav
    Davy, Aug 30, 2006
    #1
    1. Advertising

  2. Davy

    Paul Lalli Guest

    Davy wrote:
    > I used to use set (only contain one identical thing in one set) in
    > C++'s STL. I found set very useful that you can do something like
    > intersection. Is there something similar in Perl? Thanks!


    You generally want to use a hash. The keys of a hash are distinct by
    definition. If you tell us what your actual goal or problem is, we can
    give you more specific assistance...

    You may also be interested in:
    perldoc -q intersection

    my %unique;
    for (qw/foo bar baz bar foo baz baz) {
    $unique{$_} ++;
    }
    my @set = keys %unique;

    # @set will contain three elements - foo, bar, and baz (in some random
    order)

    Paul Lalli
    Paul Lalli, Aug 30, 2006
    #2
    1. Advertising

  3. ["Followup-To:" header set to comp.lang.perl.misc.]
    On 2006-08-30 13:41, Davy <> wrote:
    > I used to use set (only contain one identical thing in one set) in
    > C++'s STL. I found set very useful that you can do something like
    > intersection. Is there something similar in Perl? Thanks!


    Have you tried searching for "set" on CPAN? Set::Scalar is one of the
    first hits I get.

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Aug 30, 2006
    #3
  4. Davy

    Davy Guest

    Paul Lalli wrote:
    > Davy wrote:
    > > I used to use set (only contain one identical thing in one set) in
    > > C++'s STL. I found set very useful that you can do something like
    > > intersection. Is there something similar in Perl? Thanks!

    >

    [SNIP]
    Hi,

    Thanks! Sorry I forgot the hash table's feature.

    Best regards,
    Davy

    > You generally want to use a hash. The keys of a hash are distinct by
    > definition. If you tell us what your actual goal or problem is, we can
    > give you more specific assistance...
    >
    > You may also be interested in:
    > perldoc -q intersection
    >
    > my %unique;
    > for (qw/foo bar baz bar foo baz baz) {
    > $unique{$_} ++;
    > }
    > my @set = keys %unique;
    >
    > # @set will contain three elements - foo, bar, and baz (in some random
    > order)
    >
    > Paul Lalli
    Davy, Aug 31, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,784
  2. Lim kiang Leng
    Replies:
    3
    Views:
    215
    Ben Morrow
    Jul 18, 2004
  3. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    294
    PerlFAQ Server
    Jan 23, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    672
    PerlFAQ Server
    Feb 3, 2011
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    289
    PerlFAQ Server
    Feb 27, 2011
Loading...

Share This Page