Thread Sortierung nach Array (18 answers)
Opened by bianca at 2012-12-25 18:11

bianca
 2012-12-25 18:11
#164395 #164395
User since
2009-09-13
6978 Artikel
BenutzerIn

user image
Guten Abend und noch ein schönes Restweihnachtsfest!
Ich möchte im Script einen Hash, der als mehrspaltige Tabelle aufgebaut ist sortiert ausgeben. Dabei habe ich Sortierreihenfolge, Art der Felder und die jeweilige Richtung pro Spalte in jeweils einem Array.
Meine erste Idee war eine Schleife innerhalb von {sort}. Aber irgendwie dachte ich mir schon, dass das nicht so einfach wird.
Hier mein Versuch und weitere Details:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl -w
use strict;
use warnings;

my %uebergabe = (
    data    => {
        1   => {
            1   => 'aaron',
            2   => 'suppe',
            3   => 10,
        },
        2   => {
            1   => 'aaron',
            2   => 'zander',
            3   => 20,
        },
        3   => {
            1   => 'hannes',
            2   => 'zander',
            3   => 20,
        },
    },
    art             => ['a','a','n'],   # Art der Spalte a=Alpha oder n=Nummer
    sortierprio     => [1,2,3],         # Erst nach Spalte 1, dann nach 2, dann nach 3
    sortierricht    => [1,0,1],         # Sortierrichtung 0=abwärts/rückwärts oder 1=aufwärts/vorwärts
);
tabellenhandler(\%uebergabe);

###############################################################################

sub tabellenhandler {
    my ($input) = @_;
    foreach my $zeile (
        sort {
            for (my $sp_sort = 0; $sp_sort < scalar @{$input->{sortierprio}}; $sp_sort ++) {
                if ($input->{sortierricht}[$sp_sort]) {
                    if ($input->{art}[$sp_sort] eq 'n') {
                        $input->{data}{$a}{$input->{sortierprio{$sp_sort}}} <=> $input->{data}{$b}{$input->{sortierprio{$sp_sort}}}
                    }
                    else {
                        $input->{data}{$a}{$input->{sortierprio{$sp_sort}}} cmp $input->{data}{$b}{$input->{sortierprio{$sp_sort}}}
                    }
                }
                else {
                    if ($input->{art}[$sp_sort] eq 'n') {
                        $input->{data}{$b}{$input->{sortierprio{$sp_sort}}} <=> $input->{data}{$a}{$input->{sortierprio{$sp_sort}}}
                    }
                    else {
                        $input->{data}{$b}{$input->{sortierprio{$sp_sort}}} cmp $input->{data}{$a}{$input->{sortierprio{$sp_sort}}}
                    }
                }
            }
        }
        keys %{$input->{data}}
    ) {
        print "Zeile:";
        foreach my $spalte (
            keys %{$input->{data}{$zeile}}
        ) {
            print " - ".$input->{data}{$zeile}{$spalte};
        }
        print "\n";
    }
}

Fehlermeldungen:
Quote
Useless use of numeric comparison (<=>) in void context at sub_tabellenhandler.pl line 38.
Useless use of string comparison (cmp) in void context at sub_tabellenhandler.pl line 41.
Useless use of numeric comparison (<=>) in void context at sub_tabellenhandler.pl line 46.
Useless use of string comparison (cmp) in void context at sub_tabellenhandler.pl line 49.
Can't call method "sortierprio" without a package or object reference at sub_tabellenhandler.pl line 41.

Verliert also den Zusammenhang zwischen dem sort{} Block und dem $a/$b.
Wie muss die Syntax richtig lauten?
Bei diesen Variablen erwarte ich in der Reihenfolge die Ausgabe der Zeilen 2, 1, 3, weil in Zeile 1 und 2 aaron zwar gleich sind aber deren Spalten 2 unterschiedlich und rückwärts sortiert sein sollen, also zander vor suppe.
Danke euch

Editiert von bianca: Code korrigiert, ändert aber die Fehlermeldungen nicht
Last edited: 2012-12-25 18:24:08 +0100 (CET)
10 print "Hallo"
20 goto 10

View full thread Sortierung nach Array