in reply to Re^3: Problems with Hash Keys and Spaces
in thread Problems with Hash Keys and Spaces

I can post a small portion of it, only because it's for an internal project.
$VAR1 = [ '', ', 'Test MMA '/dir/internal_stuff
There are a few more dirs listed in the array, the 0 spot in the array is empty, 1 contains the info I need and the rest contain directories.

Replies are listed 'Best First'.
Re^5: Problems with Hash Keys and Spaces
by FunkyMonk (Bishop) on Aug 08, 2008 at 16:46 UTC
    It looks like $temp[1] has a chr 13 at the end of it. Which OS are you on?
      Unix
        Did the data in the array originate on Windows, and you chomped it? That would leave a chr 13 at the end of the string. Add
        $Data::Dumper::Useqq = 1;

        Just before the print Dumper.... If, as I suspect, it shows that $temp[1] really contains "Test MMA\r", you can use

        $temp[1] =~ s/\r//;

        to cleanse it.


        Unless I state otherwise, all my code runs with strict and warnings
Re^5: Problems with Hash Keys and Spaces
by tptass (Sexton) on Aug 08, 2008 at 17:04 UTC

    You should try creating another array with just 'Test MMA' in it and give it a try. Or perhaps, you could just try copying the following into a new file and test to see if it works. If it does than you know something is wrong with your array definition.

    my $array = ['','Test MMA','Generic key']; my %hash = ( "Common SW Component" => "4885", "Test MMA" => "5130", "Generic key" => "5033", "Another generic key" => "4575", ); my $variable = "@$array[1]"; print "$hash{$variable}\n";
Re^5: Problems with Hash Keys and Spaces
by Your Mother (Archbishop) on Aug 08, 2008 at 22:48 UTC

    Looks like you pasted/edited something funny or there were control characters messing with your cut/paste. This snippet is not legal Perl so it's not what's really there. Make sure to have use strict and use warnings on.

    $VAR1 = [ '', ', 'Test MMA '/dir/k_apps/
Re^5: Problems with Hash Keys and Spaces
by varian (Chaplain) on Aug 09, 2008 at 12:42 UTC
    The error is generated because of using a carriage return in a string used as a hash key as shown by the following code:
    #!/usr/bin/perl use strict; use warnings; my %hash = ("Common SW Component" => "4885"); my $variable = "foo\r"; print "variable ending with carrige return is =$variable=\n"; print "$hash{$variable}\n";
    Output is:
    =ariable ending with carrige return is =foo Use of uninitialized value in concatenation (.) or string at try1.pl l +ine 8.
    Update:Even without the \r it would fail, simply because the hash value is undef and an attempt is made to print an undefined value.