cosmicsoup has asked for the wisdom of the Perl Monks concerning the following question:

I am reading in a file and parsing out person first names from that file. I am using push(@Person, $fname) to load the array. Doing this however gives me multiple Jeff's, Paul's, Mary's et cetera. How can I get only one of each?

Many Thanks!
Louis

Replies are listed 'Best First'.
Re: Add distinct elements to an array
by atcroft (Abbot) on Oct 23, 2003 at 17:23 UTC
Re: Add distinct elements to an array
by Limbic~Region (Chancellor) on Oct 23, 2003 at 17:49 UTC
    Louis,
    So far, it doesn't seem like anything would prevent you from using a hash. If you are "locked in" to an array for some unspecified reason, there is Array::Unique. Keep in mind that the tied array will function much more slowly than a non-tied array.

    Cheers - L~R

Re: Add distinct elements to an array
by Anonymous Monk on Oct 23, 2003 at 17:24 UTC
    Use a hash
Re: Add distinct elements to an array
by astroboy (Chaplain) on Oct 23, 2003 at 19:39 UTC
    One way (I've put the names in an array instead of a file):
    my @source = qw(bob janne max max bob anne); my @finalnames; foreach $name (@source) { next if(grep(/^$name$/, @finalnames)); push @finalnames, $name; }
    But as the list of names gets longer, it will be less efficient. SO use a hash:
    my @source = qw(bob janne max max bob anne); my %temphash; my @finalnames; foreach $name (@source) { $temphash{$name} = 1; } @finalnames = (keys %temphash);
Re: Add distinct elements to an array
by dragonchild (Archbishop) on Oct 23, 2003 at 19:08 UTC
    Why are you doing this? Maybe if you provide more explanation of the broader context in whic you're doing this action, we might be able to provide a more comprehensive answer.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Thank you Monks. The first reply actually handled my problem well. But an explanation is warranted, especially since it may well give me insight on bettering my code.

      I am reading in data from a sybase server. Each record has an id_num, persons, and many other fields (20). Unfortunately, the way the data is captured in the database, persons are listed in a varchar(255) field by their first name and coma seperated:

      3255|bob,mary,sally,tom|2003-10-22|0|1|2003-07-01| ...

      I'm need to keep each record complete by its id_num and all other fields, but I need to do various tasks based on the persons contained in each record. One of the tasks is to produce a cgi drop down list box form that would list each person. Selecting that person would then show each project they are tasked with.

      I am reading in the line and creating an array of that field by splitting on the coma's. But again, each record has multiple persons (up to five).

      Thanks Again,
      Louis
        It sounds like you need a hash with the key being the person's name and the value being an array reference containing the idnum of each project associated with that person.

        Of course, I would strongly recommend having the person who owns the database normalize their data. You would need a project table, a person table, and another table that associates the two. That would make your life a lot easier. (And, it would make your web app a lot faster, as you wouldn't have to re-normalize the data every time.)

        ------
        We are the carpenters and bricklayers of the Information Age.

        The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

        ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.