Thread new_hash = old_hash ....desaster: Hash bekommt Elemente übertragen,... (3 answers)
Opened by xeroxed_yeti at 2007-07-19 12:30

xeroxed_yeti
 2007-07-19 12:30
#78647 #78647
User since
2007-07-19
12 Artikel
BenutzerIn
[default_avatar]
Hi zusammen,

mein Kopf qualmt und ich finde einfach nicht die Lösung! Ich hoffe ihr könnt mir helfen. Um auch euren Kopf zum rauchen zu bringen habe ich das Script minimiert, aber es beihaltet noch immer mein Problem. Ihr könnt das komplette Script kopieren, es ist lauffähig.

In dem unten aufgeführten Script habe ich einen Hash '%nodes'. Dessen Werte werden an 2 weiter Hashes, '%nodeRelation1' und '%nodeRelation2', übergeben.
Nachdem der erste Hash prozzessiert wurde stimmt die Ausgabe für '%nodeRelation1'.
Code: (dl )
1
2
3
4
5
6
7
8
existing nodeRelation1: GO:001 - red1
existing nodeRelation1: GO:002 - red2
existing nodeRelation1: GO:003 - red3

nodeRelation1: GO:001 1 red1
nodeRelation1: GO:002 1 red2
nodeRelation1: GO:003 1 red3
nodeRelation1: GO:004 0

nun startet der Teil wo '%nodeRelation2' bearbeitet wird. Auch dessen Werte lasse ich mir ausgeben.
Code: (dl )
1
2
3
4
5
6
7
existing nodeRelation2: GO:001 - blue1
existing nodeRelation2: GO:004 - blue2

nodeRelation2: GO:001 2 red1 blue1
nodeRelation2: GO:002 1 red2
nodeRelation2: GO:003 1 red3
nodeRelation2: GO:004 1 blue2


...was mich stutizg mach ist Tatsache, dass auf einmal Werte aus dem Hash '%nodeRelation1' im Hash '%nodeRelation2' auftauchen. Ich hätte viel mehr mit diser Ausgabe gerechnet:

existing nodeRelation2: GO:001 - blue1
existing nodeRelation2: GO:004 - blue2

nodeRelation2: GO:001 1 blue1
nodeRelation2: GO:002 0
nodeRelation2: GO:003 0
nodeRelation2: GO:004 1 blue2


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#/usr/bin/perl -w

use strict;
use warnings;

print  `clear`;
my %nodes = (
    'GO:001' => [0],
    'GO:002' => [0],
    'GO:003' => [0],
    'GO:004' => [0]
    );
    
my @valuesNode1 = (
    ['red1','GO:001'],
    ['red2','GO:002'],
    ['red3','GO:003']
    );
my @valuesNode2 = (
    ['blue1','GO:001'],
    ['blue2','GO:004']
    );


my %nodeRelation1 = %nodes;
foreach my $ref (@valuesNode1) {
    my $value = $ref->[0];
    my $id = $ref->[1];
    if (exists $nodeRelation1{$id}) {    
        print "existing nodeRelation1: $id - $value\n";
        # first element = scoring schmea, will be increased by 1
        ${$nodeRelation1{$id}}[0]++;
        # gene will be added to array
        push (@{$nodeRelation1{$id}}, $value);
    }        
}
print "\n";
foreach my $key (sort keys %nodeRelation1) {
    my @tmp = @{$nodeRelation1{$key}};
    print "nodeRelation1: $key\t@tmp\n";        
}
print "\n";

my %nodeRelation2 = %nodes;    
foreach my $ref (@valuesNode2) {
    my $value = $ref->[0];
    my $id = $ref->[1];
    if (exists $nodeRelation2{$id}) {
        print "existing nodeRelation2: $id - $value\n";
        # first element = scoring schmea, will be increased by 1
        ${$nodeRelation2{$id}}[0]++;
        # gene will be added to array
        push (@{$nodeRelation2{$id}}, $value);
    }        
}
print "\n";
foreach my $key (sort keys %nodeRelation2) {
    my @tmp = @{$nodeRelation2{$key}};
    print "nodeRelation2: $key\t@tmp\n";
}
print "\n";
\n\n

<!--EDIT|xeroxed_yeti|1184836959-->

View full thread new_hash = old_hash ....desaster: Hash bekommt Elemente übertragen,...