Schrift
[thread]9102[/thread]

Data::Dumper, nur etwas anders

Leser: 3


<< >> 4 Einträge, 1 Seite
Silicoid
 2007-06-20 01:01
#77669 #77669
User since
2006-04-22
3 Artikel
BenutzerIn
[default_avatar]
Hi

Erstmal ein kleines Beispielscript:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $a;

$a->[0]->{'a'}="000aaa";
$a->[0]->{'b'}="000bbb";
$a->[1]->{'a'}="111aaa";
$a->[1]->{'b'}="111bbb";
$a->[2]->{'q'}->[0]="qqqqq";


print Dumper($a);


Was als Ausgabe das zur Folge hat:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$VAR1 = [
{
'a' => '000aaa',
'b' => '000bbb'
},
{
'a' => '111aaa',
'b' => '111bbb'
},
{
'q' => [
'qqqqq'
]
}
];

Jetzt hätte ich die Ausgabe aber lieber ähnlich, wie ich die Referenz gefüllt habe, also:
Code: (dl )
1
2
3
4
5
$a->[0]->{'a'}="000aaa";
$a->[0]->{'b'}="000bbb";
$a->[1]->{'a'}="111aaa";
$a->[1]->{'b'}="111bbb";
$a->[2]->{'q'}->[0]="qqqqq";

Wobei der Output nicht unbedingt von perl ausführbar sein muß. Die ; sind mit z.B. egal. Mein Problem ist, daß ich hier eine extrem große und sehr tief verschachtelte Referenz habe und mir der Output helfen würde die Struktur etwas besser zu verstehen, bzw. Fehler zu finden.

Gibts da was?
pq
 2007-06-20 01:29
#77670 #77670
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
ein schneller hack:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sub nodes {
 my ($name, $ref, $path) = @_;
 if (ref $ref eq "ARRAY") {
   nodes($name, $ref->[$_], [@$path, ["array", $_]]) for 0..$#$ref;
 }
 elsif (ref $ref eq "HASH") {
   nodes($name, $ref->{$_}, [@$path, ["hash",$_]]) for sort keys %$ref;
 }
 else {
   print $name;
   for my $p (@$path) {
     print $p->[0] eq "array" ? "->[$p->[1]]" : "->{$p->[1]}"
   }
   print qq/ = "$ref";\n/;
 }
}
nodes(q/$a/, $a, []);

aber Data::Dumper hat auch ein paar optionen, und es gibt
sicher auch ein paar weitere cpan-module.\n\n

<!--EDIT|pq|1182288747-->
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
Silicoid
 2007-06-20 09:38
#77671 #77671
User since
2006-04-22
3 Artikel
BenutzerIn
[default_avatar]
Die Data::Dumper Optionen hab ich schon probiert, aber nichts gefunden. Ein anderes Modul hab ich leider auch nicht gefunden.

Naja, dann werd ich mir eben ein eigenes kleines Modul schrieben müssen. Sollte nicht all zu schwierig sein.
bloonix
 2007-06-20 23:06
#77672 #77672
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
CPAN:Data::TreeDumper ist um einiges übersichtlicher als Data::Dumper.

Vielleicht wäre das was für dich...
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.
<< >> 4 Einträge, 1 Seite



View all threads created 2007-06-20 01:01.