Thread Leere Arrayelemente elegant entfernen
(10 answers)
Opened by drizzo at 2008-06-28 11:02 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 #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Clone qw( clone ); my $list=[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{inhalt=>1, daten=>0},{},{},{},{},{},{},{},{},{inhalt=>2, daten=>1}]; # erste Möglichkeit: # klassisch, Speicherhungrig my $list_out_a=[]; # hier passiert es: @{$list_out_a}=grep{ref($_) eq 'HASH' and keys(%{$_}) > 0}@{$list}; print Dumper($list_out_a); # zweite Möglichkeit: # komplizierter, Speicherschonender, veränert das Original. my $list_out_b=clone($list); # hier passiert es: ref($list_out_b->[$_]) eq 'HASH' and keys(%{$list_out_b->[$_]}) == 0 and splice(@{$list_out_b},$_,1) for reverse (0..$#{$list_out_b}); print Dumper($list_out_b); # dritte Variante: # Langsam, umstänlich, aber auch eine Möglichkeit ( my $list_out_c = Dumper($list) )=~s/\$VAR1 =|\s+{},\n//sg; $list_out_c=eval($list_out_c); print Dumper($list_out_c); |