Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Duplicate Element in an Array

by vihnny (Initiate)
on Aug 21, 2012 at 20:50 UTC ( #988859=perlquestion: print w/ replies, xml ) Need Help??
vihnny has asked for the wisdom of the Perl Monks concerning the following question:

Hello PERL MONKS...

I gotta a question for you. If I had inputs entered by the user and I want to display them back but separately, how would I do it?

HERE IS AN EXAMPLE

The user enter these numbers: (1200, 1300, 1200, 1000, 1100, 1200, 1500, 1700, 2000, 2100, 3000, 2100, 1200, 1500, 1700, 1700) So I insert them in the array @input;

now I want to separate its elements if they are duplicate and create new arrays and put these items in there. For each element duplicated I create a new array and place that element in there. Example:

group one (1200, 1300, 1000, 1100, 1500, 1700, 2000, 2100, 3000)

group two (1200, 2100)

group three (1200, 1500, 1700)

group four (1200, 1700)

How can I code this?

Comment on Duplicate Element in an Array
Re: Duplicate Element in an Array
by nemesdani (Friar) on Aug 21, 2012 at 21:02 UTC
    First and most importatly, try it.
    If I understand your problem well enough ( which I'm not sure of, sorry), try to use hashes instead of arrays. If the imput number doesn't exist in the hash as a key you create it, if it does, you create another hash with the given key. Values are not important.
    Tiresome. Maybe an expert has a better solution, but it would be useful to know, what will you use these arrays or hashes for.

    I'm too lazy to be proud of being impatient.
Re: Duplicate Element in an Array
by choroba (Abbot) on Aug 21, 2012 at 21:17 UTC
    I would probably proceed like this:
    Create a hash. The numbers will be the keys, the numbers of occurrences will be the values. Once the input is finished, I would create another hash, the number of occurences being the keys, and (references to) the lists of numbers being the values.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Duplicate Element in an Array
by johngg (Abbot) on Aug 21, 2012 at 23:56 UTC

    It is not clear how you arrive at your expected output. Your group one is clear enough but I can't see the logic of how you arrive at your groups two, three and four. Perhaps you could explain further how you are separating the items. Here is a possible solution that builds an AoA by forming arrays of unique and duplicate items then recursively acting on the duplicates until none are left. It does not arrive at the same group members you show. The code.

    use strict; use warnings; use Data::Dumper; my @input = qw{ 1200 1300 1200 1000 1100 1200 1500 1700 2000 2100 3000 2100 1200 1500 1700 1700 }; my @groups = deDup( \ @input ); print Data::Dumper->Dumpxs( [ \ @groups ], [ qw{ *groups } ] ); sub deDup { my $raToCheck = shift; my $raUniq = []; my $raDups = []; my %seen; push @{ $seen{ $_ } ++ ? $raDups : $raUniq }, $_ for @$raToCheck; return $raUniq, @$raDups ? deDup( $raDups ) : (); }

    The output.

    @groups = ( [ '1200', '1300', '1000', '1100', '1500', '1700', '2000', '2100', '3000' ], [ '1200', '2100', '1500', '1700' ], [ '1200', '1700' ], [ '1200' ] );

    I hope this is helpful.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (16)
As of 2014-10-21 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (99 votes), past polls