Re: Perl Hash in String konvertieren - Problem mit uninitialisierten Variablen

David Haller <[email protected]>
Newsgroups gmane.linux.suse.programming
Message-ID <[email protected]>
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

-- 
Um die Liste abzubestellen, schicken Sie eine Mail an:
    [email protected]
Um eine Liste aller verfÃŒgbaren Kommandos zu bekommen, schicken
Sie eine Mail an: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.