Perl Hash in String konvertieren - Problem mit uninitialisierten Variablen
Hallo *, ich würde gerne eine Perl Hash in einen String konvertieren. Das mache ich derzeit so: --- snip --- #!/usr/bin/perl -w use strict; [...] sub hash2string( ) { my( $hash ) = @_; my $string = ""; my @keys = sort( keys( %$hash ) ); foreach my $key ( @keys ) { my $value = $hash->{ $key }; if( $value eq '' ) { $value = ""; } elsif( !$value ) { $value = "<NULL>"; } $string = $string . sprintf( "\t%-15s => %s\n", $key, $value ); } return substr( $string, 0, length( $string ) - 1 ); } --- snap --- Jetzt kommt es leider öfter mal vor, dass $value nicht initialisiert ist und ich erhalte dann folgende Fehlermeldung: Use of uninitialized value in string eq at ./convert.pl line 555, <STDIN> line 1. Wie kann fehlerfrei überprüfen ob $value ein leerer String ist oder nicht initialisiert wurde? Viele Grüße Bastian
Hallo, Am Thu, 05 May 2005, Bastian Schern schrieb:
ich würde gerne eine Perl Hash in einen String konvertieren. Das mache ich derzeit so:
Du kennst 'Data::Dumper'?
--- snip --- #!/usr/bin/perl -w use strict;
[...]
sub hash2string( ) { my( $hash ) = @_; my $string = "";
my @keys = sort( keys( %$hash ) ); foreach my $key ( @keys ) { my $value = $hash->{ $key }; if( $value eq '' ) { $value = ""; } elsif( !$value ) { $value = "<NULL>"; } $string = $string . sprintf( "\t%-15s => %s\n", $key, $value ); }
return substr( $string, 0, length( $string ) - 1 ); } --- snap ---
*URGS* ==== #!/usr/bin/perl -w use strict; sub hash2string(\%) { my $hash = shift; my $string = ""; foreach( sort( keys %{ $hash } ) ) { $string .= sprintf("\t%-15s => %s\n", $_, defined($hash->{$_}) ? "'$hash->{$_}'" : "undef"); } # chomp $string; ## uncomment if you *really* don't want the '\n' ## on the last element return $string; } my %hash = ( one => 'foo', two => 'bar', three => undef, four => 'baz', five => '', six => q[quux] ); print hash2string(%hash); ==== BTW: Beim Aufruf von hash2string wird der hash nicht kopiert. Siehe auch 'perldoc -f defined', 'perldoc -f chomp', 'perldoc perlsub'... Du willst vermutlich statt dem "undef" das "<NULL>" einfuegen, aber ich will oben den Unterschied zwischen leerem String defined($hash->{$_}) and $hash->{$_} eq "" ist wahr, ebenso wie $hash->{$_} eq "" aber(!) ! $hash->{$_} ist wahr und eben "undefiniert" deutlich machen. Bitte lese 'perldoc -f defined' und vergleiche mit: $ perl -we 'my $x; print "defined \$x=", defined($x) ? "true" : "false", " [$x]\n"; print "\t\$x=", $x ? "true" : "false", " [$x]\n"; $x = ""; print "defined \$x=", defined($x) ? "true" : "false", " [$x]\n"; print "\t\$x=", $x ? "true" : "false", " [$x]\n"; $x = "0"; print "defined \$x=", defined($x) ? "true" : "false", " [$x]\n"; print "\t\$x=", $x ? "true" : "false", " [$x]\n"; $x = "a"; print "defined \$x=", defined($x) ? "true" : "false", " [$x]\n"; print "\t\$x=", $x ? "true" : "false", " [$x]\n"; $x = 0; print "defined \$x=", defined($x) ? "true" : "false", " [$x]\n"; print "\t\$x=", $x ? "true" : "false", " [$x]\n"; $x = 1; print "defined \$x=", defined($x) ? "true" : "false", " [$x]\n"; print "\t\$x=", $x ? "true" : "false", " [$x]\n"; ' Use of uninitialized value in concatenation (.) or string at -e line 2. defined $x=false [] Use of uninitialized value in concatenation (.) or string at -e line 3. $x=false [] defined $x=true [] $x=false [] defined $x=true [0] $x=false [0] defined $x=true [a] $x=true [a] defined $x=true [0] $x=false [0] defined $x=true [1] $x=true [1] -dnh -- Q: What do you call it when you really *do* get different results every time you do the same thing? A: Windows
participants (2)
-
Bastian Schern
-
David Haller