Thread Anonyme Arrays miteinander vergleichen. (8 answers)
Opened by toby at 2006-08-28 02:29

Ronnie
 2006-08-28 21:23
#69269 #69269
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Ich würde eine etwas andere Vorgehensweise empfehlen. Hier mein Ansatz:
Code: (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
#!/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);

View full thread Anonyme Arrays miteinander vergleichen.