Thread Hashkey nach Spalten sortiert ausgeben (15 answers)
Opened by bloonix at 2006-05-05 17:58

Ronnie
 2006-05-06 12:25
#65598 #65598
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
So, noch etwas mehr verdeutlicht und auf deinen Bedarf angepasst:
Code: (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

package SingleState;
use Moose;

has 'state' => (isa => 'Value', is => 'rw');
has 'value' => (isa => 'Value', is => 'rw');
has 'position' => (is => 'rw');

sub to_s {
my $self = shift;
return $self->state . "\t=>\t" . $self->value . "\n";
}

package Stat;
use Moose;

has 'states' => (isa => 'ArrayRef', is => 'rw');
has 'title' => (is => 'rw');
has 'position' => (is => 'rw');

sub add_state {
my $self = shift;
return unless @_;
push @{$self->{states}}, $_ for @_;
}

sub to_s {
my $self = shift;
my $o = $self->title ? "== " . $self->title . " ==\n" : '';
$o .= $_->to_s for (sort { $a->position <=> $b->position } @{$self->states});
return $o;
}

package StatSet;
use Moose;
use Data::Dumper;

has 'stats' => (isa => 'ArrayRef', is => 'rw');

sub add_stat {
my $self = shift;
return unless @_;
push @{$self->{stats}}, $_ for @_;
}

sub report {
my $self = shift;
my $s;
$s .= $_->to_s . "\n" for (sort { $a->position <=> $b->position } @{$self->stats});
return $s;
}

package main;

my $set = new StatSet;

my $stat1 = new Stat ( title => 'CPU' );
my $stat2 = new Stat ( title => 'Memory' );

my $state1 = new SingleState( state => 'Idle', value => '92', position => 1 );
my $state2 = new SingleState( state => 'User', value => '0', position => 2 );
my $state3 = new SingleState( state => 'Cached', value => '531976', position => 1 );


$stat1->add_state( $state1, $state2 );
$stat1->position(1);

$stat2->add_state( $state3 );
$stat2->position(2);

$set->add_stat($stat1);
$set->add_stat($stat2);

print $set->report;

Ausgabe beispielhaft:
Code: (dl )
1
2
3
4
5
6
== CPU ==
Idle => 92
User => 0

== Memory ==
Cached => 531976

View full thread Hashkey nach Spalten sortiert ausgeben