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

 my $variable = "$temp[1]";

Replies are listed 'Best First'.
Re^3: Problems with Hash Keys and Spaces
by FunkyMonk (Bishop) on Aug 08, 2008 at 16:36 UTC
    So, What's in $temp[1]?

    Add

    use Data::Dumper;

    Near the top of your program, and

    print Dumper \@temp;

    just before the line with the error


    Unless I state otherwise, all my code runs with strict and warnings
      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.
        It looks like $temp[1] has a chr 13 at the end of it. Which OS are you on?

        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";

        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/
        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.
      $VAR1 = [ '', ', 'Test MMA '/dir/k_apps/
      In the array, slot 0 is empty, 1 has the info I want to be matched with the key, and the rest are all component directories. Thanks
Re^3: Problems with Hash Keys and Spaces
by repellent (Priest) on Aug 08, 2008 at 17:38 UTC
    Don't interpolate "$temp[1]". You may be losing information due to interpolation. Try this instead:
    my $variable = $temp[1];