1
2
3
4
5
6
7
8
%hash = ( "DEVICE_1" => { "1" => "VLAN_1",
"2" => "VLAN_2"},
"DEVICE_2" => { "1" => "VLAN_1",
"2" => "VLAN_2",
...
"100" => "VLAN_100",
},
);
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
sub BUILD_REQUESTS {
foreach my $device (keys %hash) {
foreach my $vlan (keys %{$hash{$device}}) {
=> durchlaufe es genau 10x
=> übergebe $device und $vlan an eine Subroutine,
welche die SNMP Objekte erstellt
=> entferne $vlan key aus Hash, damit diese nicht wieder
aufgerufen werden können ???
}
}
snmp_dispatcher(); => damit werden die SNMP Abfragen an die Devices
durchgeführt (aus den zuvor erstellten SNMP
Objekten)
! danach müsste nach meinem Verständis geprüft werden, ob sich noch
VLANs Keys in den Unterhashes befinden...richtig???
! wenn JA, dann müsste sich ja die komplette Funktion "sub
BUILD_REQUESTS" noch mal aufrufen und zwar solange, bis es nichts mehr
gibt!? Oder wie würde das ein Perl Profi machen? Mit Rekursion in den
Schleifen schon???
}
1 2 3 4 5
my @var = 1 .. 38; while ( @var ) { my @splice = splice @var, 0, 10; print "@splice\n"; }
1 2 3 4 5 6 7 8 9
sub BUILD_REQUESTS { foreach my $device (keys %hash) { my @vlans = keys %{$hash{$device}}; while ( @vlans ) { my @splice = splice @vlans, 0, 10; # mache jetzt etwas mit den 10 vlans in @splice } } }
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
#! /usr/bin/perl # vim:ts=4 sw=4 sts=4 et nu fdc=3: use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; # data structure: Hash of Arrays # Device => [ list of vlan IDs ] my %vlan = ( device_1 => [ 1..38 ], device_2 => [ 1..48 ], device_3 => [ 1..24 ], ); # take 10 items of each device sub hash # process items # redo until all sub hashs are empty while ( 1 ) { my %data = (); for my $dev ( keys %vlan ) { # skip if no vlan IDs left for current device next if 0 == @{ $vlan{$dev} }; # extract next 10 IDs of current device $data{$dev} = [ splice( @{$vlan{$dev}}, 0, 10 ) ]; } # nothing extracted? we are finished last if 0 == keys %data; # process extracted data print Dumper( \%data ); } __END__
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
#! /usr/bin/perl # vim:ts=4 sw=4 sts=4 et nu fdc=3: use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; # data structure: Hash of Arrays # Device => [ list of vlan IDs ] my %vlan = ( device_1 => [ 1..38 ], device_2 => [ 1..48 ], device_3 => [ 1..24 ], ); # take 10 items of each device sub hash # process items # redo until all sub hashs are empty my %data; do { %data = (); for my $dev ( keys %vlan ) { # skip if no vlan IDs left for current device next if 0 == @{ $vlan{$dev} }; # extract next 10 IDs of current device $data{$dev} = [ splice( @{$vlan{$dev}}, 0, 10 ) ]; } # process extracted data print Dumper( \%data ); } while ( keys %data ); __END__
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
#! /usr/bin/perl # vim:ts=4 sw=4 sts=4 et nu fdc=3: use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; # data structure: Hash of Arrays # Device => [ list of vlan IDs ] my %vlan = ( device_1 => [ 1..38 ], device_2 => [ 1..48 ], device_3 => [ 1..24 ], ); sub fetch_data { my $hRef = shift; my %data; for my $dev ( keys %$hRef ) { # skip if no vlan IDs left for current device next if 0 == @{ $vlan{$dev} }; # extract next 10 IDs of current device $data{$dev} = [ splice( @{$vlan{$dev}}, 0, 10 ) ]; } return %data; } # take 10 items of each device sub hash: via fetch_data() # process items: print Dumper( \%data ) # redo until all sub hashs are empty while ( my %data = fetch_data( \%vlan ) ) { print Dumper( \%data ); } __END__