Schrift
[thread]9203[/thread]

Ausgabe eines (komplexeren) Hashes: %hash (Seite 2)

Leser: 3


<< |< 1 2 3 >| >> 22 Einträge, 3 Seiten
Duff
 2007-07-19 11:51
#78612 #78612
User since
2006-10-06
283 Artikel
BenutzerIn

user image
[quote=renee,19.07.2007, 09:21][quote=renee,18.07.2007, 22:48]*Wie* soll denn die Ausgabe aussehen?[/quote]
*Nochmal frag*[/quote]
Hatte mir die Ausgabe in etwa so vorgestellt wie die von Dumper.

Also:

Schlüssel1: Schlüssel1.1: Wert1
Schlüssel1: Schlüssel1.1: Wert2
Schlüssel1: Schlüssel1.2: Wert1
Schlüssel1: Schlüssel1.2: Wert2
....
Schlüssel2: Schlüssel1.1: Wert1
Schlüssel2: Schlüssel1.1: Wert2
Schlüssel2: Schlüssel1.2: Wert1
Schlüssel2: Schlüssel1.2: Wert2

Oder gibt es noch besser Darstellungsmöglichkeiten?
D'OH
Daniel
bloonix
 2007-07-19 11:52
#78613 #78613
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Also ich bevorzuge zur Ausgabe gerne auch mal CPAN:Data::TreeDumper.

Hier eine Beispielausgabe von DumpTree().

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
print DumpTree($hash, 'Dein Titel', DISPLAY_ADDRESS => 0);

Dein Titel
`- CpuStats
  |- cpu
  |  |- idle = 100.00
  |  |- iowait = 0.00
  |  |- nice = 0.00
  |  |- system = 0.00
  |  |- total = 0.00
  |  `- user = 0.00
  `- cpu0
     |- idle = 100.00
     |- iowait = 0.00
     |- nice = 0.00
     |- system = 0.00
     |- total = 0.00
     `- user = 0.00
\n\n

<!--EDIT|opi|1184831833-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
bloonix
 2007-07-19 11:55
#78614 #78614
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Vielleicht mit CPAN:YAML?

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use YAML qw(Dump);

print Dump($stat);

---
CpuStats:
cpu:
idle: 100.00
iowait: 0.00
nice: 0.00
system: 0.00
total: 0.00
user: 0.00
cpu0:
idle: 100.00
iowait: 0.00
nice: 0.00
system: 0.00
total: 0.00
user: 0.00
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
Relais
 2007-07-19 12:02
#78615 #78615
User since
2003-08-06
2244 Artikel
ModeratorIn
[Homepage] [default_avatar]
Na dann werfe ich noch CPAN:Data::Dump::Streamer (DDS) in den Raum.
Code: (dl )
1
2
use DDS;
Dump($ref);

Im Handbuch gibt es dann noch eine Übersicht über Dumper-Module im "SEE ALSO" ;-)
Erst denken, dann posten --
26. Deutscher Perl- u. Raku -Workshop 15. bis 17.04.2024 in Frankfurt/M.

Winter is Coming
bloonix
 2007-07-19 12:11
#78616 #78616
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
[quote=Relais,19.07.2007, 10:02]Im Handbuch gibt es dann noch eine Übersicht über Dumper-Module im "SEE ALSO" ;-)[/quote]
Quote
Data::Dump::Streamer - The best perl dumper. But I would say that. :-)


das war ja klar :)
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
Duff
 2007-07-19 12:30
#78617 #78617
User since
2006-10-06
283 Artikel
BenutzerIn

user image
Also ich würde es mal gerne über normale Schleifen (foreach und so) ausgeben lassen.

Doch leider verstehe ich nicht ganz, wie ich nach dem Anfang:

Code: (dl )
1
2
3
foreach my $k (sort %hash) {
# wie genau weiter?
}


fortfahren muss?\n\n

<!--EDIT|Duff|1184833835-->
D'OH
Daniel
renee
 2007-07-19 12:44
#78618 #78618
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code: (dl )
1
2
3
4
5
foreach my $k (sort keys %hash) {
for my $subkey ( sort keys %{ $hash{$k} } ){
print $k,":",$subkey,":",$hash{$k}->{$subkey},"\n";
}
}


Sowas eignet sich aber nur wenn Du *genau weißt* wie Deine Datenstruktur aussieht. In diesem Fall also ein Hash mit Hashreferenzen, die Skalare als Value haben.

Mehr zu Referenzen: perlreftut
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-19 12:51
#78619 #78619
User since
2006-10-06
283 Artikel
BenutzerIn

user image
Ja genau sowas meine ich.

Das Ergebnis ist fast richtig.
Code: (dl )
1
2
3
4
5
6
7
8
schluessel1:Schluessel1.1:ARRAY(0x8063568)
schluessel1:Schluessel1.2:ARRAY(0x80ed310)
schluessel1:Schluessel1.3:ARRAY(0x80f3818)
...
schluessel3:Schluessel3.1:ARRAY(0x8063468)
schluessel3:Schluessel3.2:ARRAY(0x80ed110)
schluessel3:Schluessel3.3:ARRAY(0x80f1818)
...
D'OH
Daniel
bloonix
 2007-07-19 13:07
#78620 #78620
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Vielleicht sowas?

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
output(\%hash);

sub output {
  my $hash = shift;
  foreach my $key (sort keys %$hash) {
     if (ref($hash->{$key})) {
        output($hash->{$key}, @_, $key);
        next;
     }  
     print join(' : ', @_, $key, $hash->{$key}), "\n";
  }  
}


Das klappt natürlich nur mit HoH.\n\n

<!--EDIT|opi|1184836115-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
renee
 2007-07-19 13:09
#78621 #78621
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
aus
Code: (dl )
print $k,":",$subkey,":",$hash{$k}->{$subkey},"\n";


mach
Code: (dl )
print $k,":",$subkey,":",$_,"\n" for @{$hash{$k}->{$subkey}};
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 3 >| >> 22 Einträge, 3 Seiten



View all threads created 2007-07-18 17:17.