Leser: 24
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
#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw/Dumper/; my @pairs_in_db = ([1,2],[1,9],[3,4],[5,6]); my @pairs_from_somewhere_else = ([1,2],[5,6],[7,18]); my %map = (); foreach my $pair ( @pairs_in_db ) { $map{$pair->[0]}->{$pair->[1]} = 1; } my %keep = (); foreach my $pair ( @pairs_from_somewhere_else ) { if( exists $map{$pair->[0]} && exists $map{$pair->[0]}->{$pair->[1]} ) { printf("<%s, %s> was in db\n", @{$pair}); $keep{$pair->[0]}->{$pair->[1]} = 1; }else{ printf("<%s, %s> has to be inserted into the databse\n", @{$pair}); } } foreach my $pair ( @pairs_in_db ) { unless( exists $keep{$pair->[0]} && exists $keep{$pair->[0]}->{$pair->[1]} ) { printf("<%s, %s> will be removed\n", @{$pair}); } }
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
#!/usr/bin/perl use strict; use warnings; my @pairs_in_db = ([1,2],[1,9],[3,4],[5,6]); my @pairs_from_somewhere_else = ([1,2],[5,6],[7,18]); #prepare map my %map; # set original and a counter $map{join(',',@$_)}=[$_,0] for(@pairs_in_db); for(@pairs_from_somewhere_else) { my $str=join(',',@$_); if(exists($map{$str})) { $map{$str}->[1]++; printf("<%02u, %02u> was in db\n", @{$map{$str}->[0]}); } else { printf("<%02u, %02u> has to be inserted into the databse\n", @$_); } } for(values(%map)) { unless($_->[1]) { printf("<%02u, %02u> will be removed\n", @{$_->[0]}); } }
2010-09-09T21:29:25 pktmJetzt will
a) ich all die Paare, die der Nutzer angegeben hat und die noch nicht in der DB waren, in die DB einfügen und
b) alle Paare, die der Nutzer nicht angegeben hat, aus der DB löschen.
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw/Dumper/;
my @pairs_in_db = ([1,2],[1,9],[3,4],[5,6]);
my @pairs_from_somewhere_else = ([1,2],[5,6],[7,18]);
my @pairs_to_delete=@pairs_in_db;
my @pairs_to_create;
NEW:
for my $new (@pairs_from_somewhere_else) {
for my $old (@pairs_to_delete) {
if (defined($old) and "@$new" eq "@$old" ) {
$old=undef;
next NEW;
}
}
push @pairs_to_create,$new;
}
print Dumper \@pairs_to_create,\@pairs_to_delete;
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
#!/usr/bin/perl use strict; use warnings; sub pair_hash { return $_[0][0] . ',' . $_[0][1]; } sub array_union { my ($a, $b) = @_; my %bhash = map { &pair_hash($_) => 1 } @{$b}; return grep { defined $bhash{ &pair_hash($_) } } @{$a}; } sub array_diff { my ($a, $b) = @_; my %bhash = map { &pair_hash($_) => 1 } @{$b}; return grep { !defined $bhash{ &pair_hash($_) } } @{$a}; } my @pairs_in_db = ([1,2],[1,9],[3,4],[5,6]); my @pairs_from_somewhere_else = ([1,2],[5,6],[7,18]); my @was_in_db = &array_union(\@pairs_in_db, \@pairs_from_somewhere_else); my @pairs_to_delete = &array_diff(\@pairs_in_db, \@pairs_from_somewhere_else); my @pairs_to_create = &array_diff(\@pairs_from_somewhere_else, \@pairs_in_db); foreach my $ext (@was_in_db) { printf("<%s, %s> was in db\n", @{$ext}); } foreach my $ins (@pairs_to_create) { printf("<%s, %s> has to be inserted into the databse\n", @{$ins}); } foreach my $del (@pairs_to_delete) { printf "<%s, %s> will be removed\n", @{$del}; }
2010-09-10T06:44:02 vstmJa, mit Mengenoperationen könnte man das ziemlich schön formulieren. Habe nach kurzem Googlen keine vorhandene Lib gefunden, [...]
"@$a" eq "@$b"
($a,$b)=($b,$a)
(@a)==(@b)