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

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

Replies are listed 'Best First'.
Re^4: Problems with Hash Keys and Spaces
by Anonymous Monk on Aug 08, 2008 at 16:43 UTC
    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?
        Unix

      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.
Re^4: Problems with Hash Keys and Spaces
by Anonymous Monk on Aug 08, 2008 at 16:46 UTC
    $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