Leser: 3
9 Einträge, 1 Seite |
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
#!/usr/bin/perl
use strict;
use warnings;
# Master-Array
my $master = [
{ x => ['1','a'], y => [], z => ['4'], },
{ x => ['b','a'], y => ['1','a'], z => ['1'], },
{ x => ['1','a'], y => [], z => [1], },
{ x => [], y => ['a','1'], z => ['2'], },
];
# autonome Hashes
my $h1 = { x => ['1','a'] , y => [] , z => ['4'] };
my $h2 = { x => ['a'] , y => [] , z => ['1'] };
my $h3 = { x => ['1'] , y => ['a',1] , z => [2] };
my $h4 = { x => [] , y => ['a1'] , z => ['1','2'] };
my @checkit = ($h1,$h2);
my $hrefCount;
foreach my $href (@checkit){
$hrefCount++;
masterElemente($href);
printf "ende von hrefCount: %s\n",$hrefCount;
};
sub masterElemente{
my $href = shift;
for my $element (0..$#{$master}){
printf "MASTERELEMENT: %s\n",$element;
my $count=0;
my $masterKeys = keys (%{@{$master}[$element]});
foreach my $key (keys %$href){
printf "key: $key\n";
printf "\tmaster:\t@{$master->[$element]->{$key}}\n";
printf "\thref:\t@{$href->{$key}}\n";
next if ( ($href->{$key}) != ($master->[$element]->{$key}) );
printf "count=%s\n",$count;
$count++;
}
printf "\n";
printf "BINGO beim $element element (gezaehlt von 0-$#{$master})!\n" if $masterKeys eq $count;
printf "\n";
last if $masterKeys eq $count;
}
}
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Indent=0;
# Master-Array
my $master = [
{ x => ['1','a'], y => [], z => ['4'], },
{ x => ['b','a'], y => ['1','a'], z => ['1'], },
{ x => ['1','a'], y => [], z => [1], },
{ x => [], y => ['a','1'], z => ['2'], },
];
# autonome Hashes
my $h1 = { x => ['1','a'] , y => [] , z => ['4'] };
my $h2 = { x => ['a'] , y => [] , z => ['1'] };
my $h3 = { x => ['1'] , y => ['a',1] , z => [2] };
my $h4 = { x => [] , y => ['a1'] , z => ['1','2'] };
my @checkit = ($h1,$h2,$h3,$h4);
my $hrefCount;
foreach my $href (@checkit){
$hrefCount++;
masterElemente($href);
printf "ende von hrefCount: %s\n",$hrefCount;
};
sub masterElemente
{
my $href = shift;
my $skey_suche=Dumper($href);
for my $pos (0..$#{$master})
{
my $skey_master=Dumper($$master[$pos]);
if($skey_master eq $skey_suche)
{
print "BINGO im Element an der Position $pos (".@{$master}." Elemente)!\n";
last;
}
}
}
$Data::Dumper::Sortkeys=1;
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
#!/usr/bin/perl
use strict;
use warnings;
package State;
use Moose;
has 'x' => ( is => 'rw', isa => 'ArrayRef' );
has 'y' => ( is => 'rw', isa => 'ArrayRef' );
has 'z' => ( is => 'rw', isa => 'ArrayRef' );
sub compare {
my $self = shift;
my $other = shift;
my $is_equal = 1;
for (sort keys %$self) {
$is_equal = 0 unless _arr_eq( $self->{$_}, $other->{$_} )
}
return $is_equal;
}
sub _arr_eq {
my $arr1 = shift;
my $arr2 = shift;
return '0' if $#$arr1 != $#$arr2;
my $is_equal = 1;
for (0 .. $#$arr1) {
$is_equal = 0 unless ($arr1->[$_] eq $arr2->[$_]);
}
return $is_equal;
}
package main;
use Data::Dumper;
my $master = new State ( x => ['1','a'], y => [], z => ['4'], );
my $h1 = new State ( x => ['1','a'], y => [], z => ['4'] );
my $h2 = new State ( x => ['a'], y => [], z => ['1'] );
print "\$h1 is identical.\n" if $master->compare($h1);
print "\$h2 is identical.\n" if $master->compare($h2);
1
2
3
4
5
6
7
8
# ...
package State;
use Moose;
use overload '==' => \&compare;
# ...
print "\$h1 is identical.\n" if $master == $h1;
print "\$h2 is identical.\n" if $master == $h2;
9 Einträge, 1 Seite |