Schrift
[thread]9175[/thread]

Use of uninitialized value ...: Hash mit Array als value (undef) (Seite 2)

Leser: 2


<< |< 1 2 >| >> 15 Einträge, 2 Seiten
Duff
 2007-07-11 12:31
#78352 #78352
User since
2006-10-06
283 Artikel
BenutzerIn

user image
Die Sortierung sollte so aussehen, dass zuerst nach den values sortiert und dann nach den keys sortiert wird.

Beispiel.

Key1 = value1
Key3 = value2
Key4 = value3
Key2 = value4
...
D'OH
Daniel
Ronnie
 2007-07-11 12:43
#78353 #78353
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl

use strict;
use warnings;

my %hash = (
    '[foo]' => [3],
    '[bar]' => [2],
    '[buz]' => [1],
    '[qiz]' => [undef],
);

print 'K: '. $_ .' V: '. $hash{$_}->[0] . "\n" for 
    sort { $hash{$a}->[0] <=> $hash{$b}->[0] } 
    grep { defined $hash{$_}->[0] } 
    keys %hash;
renee
 2007-07-11 14:53
#78354 #78354
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code (perl): (dl )
sort { $hash{$a}->[0] <=> $hash{$b}->[0] or $a cmp $b }
sortiert dann noch nach den Keys...

@Duff: Das nächste mal bitte die Ausgangsstruktur und das Ergebnis kurz beschreiben, dann kann man auch besser helfen.
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
Duff
 2007-07-12 10:15
#78355 #78355
User since
2006-10-06
283 Artikel
BenutzerIn

user image
Ok, sorry. Habe mich wohl ein wenig unverständlich ausgedrückt.

Aber wie muss ich dass ganze jetzt bei mir einbauen?

So funktioniert es nicht:
Code: (dl )
1
2
3
4
5
6
7
for my $k (sort { $hash{$a}->[0] <=> $hash{$b}->[0] or $a cmp $b } keys %hash) {
my @arr = grep { defined $_ } @{ $hash{$k} };

if (@arr) {
printf "%-25s : %-80s\n", $k, "@arr";
}
}
D'OH
Daniel
renee
 2007-07-12 10:55
#78356 #78356
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
*WAS* funktioniert nicht??

Das hier läuft einwandfrei:
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
#!/usr/bin/perl

use strict;
use warnings;

my %hash = (
  key1 => ['dies'],
  ist  => ['ein'],
  Test => ['hallo'],
  was => ['wert'],
  los  => ['hash'],
);



for my $k (sort { $hash{$a}->[0] cmp $hash{$b}->[0] or $a cmp $b } keys %hash) {
       my @arr = grep { defined $_ } @{ $hash{$k} };

       if (@arr) {
            printf "%-25s : %-80s\n", $k, "@arr";
       }
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
<< |< 1 2 >| >> 15 Einträge, 2 Seiten



View all threads created 2007-07-10 16:56.