Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

creating unique list from elements in an array

by rimmern (Acolyte)
on Aug 17, 2001 at 12:31 UTC ( #105634=perlquestion: print w/replies, xml ) Need Help??

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

I have a file which contains lots of lines whose values are separated by | ie:

value 1|value 2|tom, chris, dave|value 4
value 1|value 2|peter, tom, dave|value 4

what I want to do is find all the unique names in all the instances of value3 in the file (ie tom, chris, dave & peter).

I know how to do it if the names are in an array but not if they are in an element in an array...

Any suggestions ?

Cheers, Digsy

  • Comment on creating unique list from elements in an array

Replies are listed 'Best First'.
Re: creating unique list from elements in an array
by mirod (Canon) on Aug 17, 2001 at 12:52 UTC

    The word unique nearly always call for a hash to be used. The solutiom is to extract the names, then to use them as (unique) keys to a hash.

    #!/bin/perl -w use strict; my %unique_names; # this is the hash while( <DATA>) { my @values= split /\|/; # extract the fields (don't + forget to backslash the |) my @names= split /,\s*/, $values[2]; # extract the names foreach my $name (@names) { $unique_names{$name}++; } # we don't really care abou +t the value, just the key } my @unique_names= keys %unique_names; # put the names back into a +n array print join "-", @unique_names; print "\n"; __DATA__ value 1|value 2|tom, chris, dave|value 4 value 1|value 2|peter, tom, dave|value 4

      Very nice, mirod++, a good explanation! I just add the compact version - which is also a bit more efficient because it uses a hash slice:

      my %unique; @unique{ split /,\s*/, ( split /\|/ )[2] } = () while (<DATA>); my @unique_names = keys %unique; print join "-", @unique_names; print "\n"; __DATA__ value 1|value 2|tom, chris, dave|value 4 value 1|value 2|peter, tom, dave|value 4

      -- Hofmator

Re: creating unique list from elements in an array
by mugwumpjism (Hermit) on Aug 17, 2001 at 15:17 UTC

    Is it a one-liner you want?

    perl -ne '($a{$_} = 1) foreach split /,\s*/, (split /\|/)[2]; END { @a=sort keys %a; print "@a\n" }'

    Shorter in shell :

    awk -F\| '{print $3}' | sed 's/, */\ /g' | sort -u
Re: creating unique list from elements in an array
by Sifmole (Chaplain) on Aug 17, 2001 at 17:04 UTC
    mirod's answer is well explained, and Hofmator and mugwumpjism (what a beaut of a name) have shown how Perl can make something very... um.. concise.

    I noticed that you are a novice monk and therefor might not be aware of the great deal of information that is available for you via the Search box at the top of your screen while on PerlMonks. Typing the words "unique elements" in the box returned many articles that contain a great deal of information related to your question.

    One of the benefits of using the search would be that you would not have to wait for an answer to your question, it is already there. You might also find out quite a bit more that you had originally asked.

    Good luck, welcome, and don't forget the Search box.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105634]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2023-01-30 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?