0
my $dat;
$dat{"1","1","1","1","1"}{'CR'} = "2";

foreach my $k1 (keys %dat){
    print $k1;
}

This is my code and it should be no problem and can execute in other computer but my laptop show this result
enter image description here

It compile with no error..... can somone tell me what happen? Is it less certain module?

xvi_16
  • 115
  • 1
  • 1
  • 10
  • 2
    Unrelated to the actual question, but note that `$dat{...}` accesses the variable `%dat`, *not* `$dat`. `my $dat` should, therefore, be `my %dat` instead. (If you had `use strict` enabled, it would have caught that for you.) Aside from that, I am not able to reproduce your output - I get `11111`. – Dave Sherohman Feb 18 '15 at 08:24
  • that my mistake but still i think should can produce 11111 but i not sure y it print something weird – xvi_16 Feb 18 '15 at 08:37

2 Answers2

5

You may want to check perlvar

$; The subscript separator for multidimensional array emulation. If you refer to a hash element as

$foo{$a,$b,$c}

it really means

$foo{join($;, $a, $b, $c)}

Default is \034, the same as SUBSEP in awk. If your keys contain binary data there might not be any safe value for $;.

use strict;
use warnings;

my %dat;
$dat{"1","1","1","1","1"}{'CR'} = "2";
# which is equivalent to
# my $k = join($;, "1","1","1","1","1");
# $dat{$k}{'CR'} = "2";

foreach my $k1 (keys %dat){
    print "$_\n" for map { $_ eq $; ? '$;' : $_ } split //, $k1;
}

output

1
$;
1
$;
1
$;
1
$;
1
mpapec
  • 50,217
  • 8
  • 67
  • 127
2

Your hash does not contain what you think it contains. Specifically, it has only a single key:

#!/usr/bin/env perl    

use strict;
use warnings;

my %dat;
$dat{"1","1","1","1","1"}{'CR'} = "2";

use Data::Dumper; 
print Dumper \%dat

Output:

$VAR1 = {
          '11111' => {
                           'CR' => '2'
                         }
        };

As suggested by Сухой27, the 1s in the key are concatenated using the value of $; as a separator:

#!/usr/bin/env perl    

use strict;
use warnings;

$; = '---';

my %dat;
$dat{"1","1","1","1","1"}{'CR'} = "2";

use Data::Dumper; 
print Dumper \%dat

Output:

$VAR1 = {
          '1---1---1---1---1' => {
                                   'CR' => '2'
                                 }
        };

Change $; to an empty string, and you'll get 11111 as output, as I assume you're expecting.

Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102