[Perl] Array mit Hashes verwalten

daniel_greifswald

Grünschnabel
Hallo, also ich habe die Aufgabe bekommen, dass ich mehrere Namenslisten als Input hab und diese nun verarbeiten soll und diese Ausgabe erzeugen soll:

Input :
setname1: setname2: setname3:
Daniel Gerlinde Ralf
Lars Ifran Lars
Stephanie Stephanie Daniel
Noah Lars Katharina
Nikolai Ifran
Mario

Output :

individual set sizes:
set 1 setname1 : 6
set 2 setname2 : 4
set 3 setname3 : 5
____________________________
total (union of sets) : 10


power combination
set 1 2 3 | #of elements
____________________________________
combination 1 0 0 | 3
combination 0 1 0 | 1
combination 1 1 0 | 1
combination 0 0 1 | 2
combination 1 0 1 | 1
combination 0 1 1 | 1
combination 1 1 1 | 1

Explanation:
1: is in the set
0: is not in set
For example, if you had n = 3 sets, then the line
combination 1 0 0
Shows the number of items that are in set 1 but neither in set 2 nor in set 3. All 2^n-1 = 7 combinations add up to the total number of elements.


Mein Skript :

Perl:
#!/usr/bin/perl
use strict;
use Getopt::Long;

my %setList; 
my @completeList;
my $n = scalar @ARGV;

for(my $i=0; $i<$n; $i++){
    %setList = 0;
    open (SET, "<", $ARGV[$i]) or die ("Could not open file $ARGV[$i].\n");
    #print "opened file $ARGV[$i]\n";
    my %setList = <SET>;
    foreach my $key (keys %setList){
        @completeList[$i]->{$key} = '1';
    }
    push (@completeList, \%setList);
    close SET;   
}


Jedoch komme ich nicht ganz mit der Datenstruktur des Hashes klar und weiß nicht wie ich es am besten nun schreibe.
Ist es Sinnvoll ein Array mit einem Hash für jeden Setnamen zuerstellen?
Möchte also @completeList + Hash von jedem Namen mit Wertzuweisung 1 (oder Nummer des Sets) zuweisen.

Ziemlich viel Text, entschuldigt bitte.
Danke im Voraus.

Daniel
 

Neue Beiträge

Zurück