ScOut3R has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I've got an error if i'd like to print out a hash of hashes with a reference.
sub print_host_config { print Dumper($host_configref); foreach my $key ( sort keys %$host_configref ) { print "$key.\n"; print "Host = $host_configref->{$key}{cmd}\n"; } }
But it says that:
Use of uninitialized value in concatenation (.) or string at...

The Dumper function works, so the hash of hashes is populated with the data correctly.

Replies are listed 'Best First'.
Re: print hash of hashes with a reference
by FunkyMonk (Bishop) on Aug 16, 2008 at 21:02 UTC
    You're not giving us much to work with. Can you show us what print Dumper($host_configref) produced?

    Generally, you'll find it much more productive if you tell us:

    1. This is what my data looks like
    2. This is what I want output
    3. This is what I've tried

    Obviously, you've given us 3, and 2 isn't hard to work out, but we need 1 unless you want us to guess at your problem.

      Sorry, here's the output of Dumper:
      $VAR1 = { '4' => { 'group ' => ' 3 ', 'user ' => ' root ', 'host ' => ' 10.0.200.181 ', 'cmd ' => ' ls -l / ' }, '1' => { 'cmd ' => ' md5sum /vmlinuz ', 'group ' => ' 1 ', 'user ' => ' root ', 'host ' => ' 10.0.200.181 ' }, '3' => { 'group ' => ' 1 ', 'user ' => ' root ', 'host ' => ' 10.0.200.183 ', 'cmd ' => ' ls -l / ' }, '2' => { 'group ' => ' 2 ', 'user ' => ' root ', 'host ' => ' 10.0.200.184 ', 'cmd ' => ' ls -l / ' } }; 1. Use of uninitialized value in concatenation (.) or string at ./master- +backup-daemon.pl line 245. Host = 2. Use of uninitialized value in concatenation (.) or string at ./master- +backup-daemon.pl line 245. Host = 3. Use of uninitialized value in concatenation (.) or string at ./master- +backup-daemon.pl line 245. Host = 4. Use of uninitialized value in concatenation (.) or string at ./master- +backup-daemon.pl line 245. Host =
      I'd like to print values from the hashes, but somehow the way i want to extract the values from the hashes doesn't seem to work. Line 245 is the line where the print command is.
        It looks like you have spaces after the "cmd" string. So, your keys are not really "cmd", they are "cmd ". I can't really tell how many spaces you have in those keys.
        Look at your Data::Dumper output. You've got a space or tab after each of your keys.
        'cmd ' => ' ls -l / '

        Looks like your keys contain trailing whitespace, and your values have a leading blank and a trailing newline.

        -- shmem
        Notice that the second level keys have whitespace at the end, and notice that your print statement uses a key of cmd (no whitespace). You should trim the whitespace when you construct the hash.

        You should also take notice here that the values have \n (and possibly other whitespace) at the end (which may also bite you, depending on how you intend to use them).

        Thank You! All of You! The spaces were the problem. It's a shame that i haven't noticed this myself.