Thread Auslesen von Hash-Keys und selbstaufrufende Funktion
(3 answers)
Opened by reen_sc at 2011-09-15 15:21
Warum muss es eigentlich eine sich selbst aufrufende Funktion sein?
Lösungsvorschlag ohne Rekursion: 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 #! /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__ Noch eine Variante mit anderer Schleife ... 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 #! /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__ Und noch eine mit eigener Subroutine und "normaler" Schleife: 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 #! /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__ edit1: $D::D::Sortkeys im Code eingefügt edit2:weitere Variante eingefügt Last edited: 2011-09-17 09:38:42 +0200 (CEST) meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen! |