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

hello i have this code :

my $sample_cds_genotype = $prefixe ."_cds_genotype"; my $sample_cds_RT = $prefixe ."_cds_RT"; my $sample_cds_R1 = $prefixe ."_cds_R1"; my $sample_cds_R2 = $prefixe ."_cds_R2"; my %hash_sample_cds_genotype; my %hash_sample_cds_RT; my %hash_sample_cds_R1; my %hash_sample_cds_R2; my %hash_geno_group; my %hash_read_p1_count; my %hash_read_p2_count; #alimentation du hash des groupe genotypique connu pour les echatillo +ns open(GENOTYPED,"<$list_geno") or die ("Cannot open $list_geno\n"); while (defined(my $_ = <GENOTYPED>)) { my @geno_group_infos = split(m/\t/,$_); next if ($_ =~ m/^ECHANTILLON/); chomp($_); my $sample_nm = $geno_group_infos[0]; my $sample_geno_group = $geno_group_infos[1]; $hash_geno_group{$sample_geno_group}{"sample"}=$sample_nm; + } close(GENOTYPED); print Dumper(\%hash_geno_group); #String des fichiers inputs foreach my $fichier (@fichiers) { my $filename = $fichier; my $sample_name; #alimentation des hashs de resultat avec cet echatillon open(RESULTAT,"<$filename") or die ("Cannot open $filename\n"); while (defined(my $_ = <RESULTAT>)) { $sample_name = $filename."\t"; #my @attribut_name = split(m/out_/,$sample_name); #$sample_name = $attribut_name[1]."\t"; my @resutat_infos = split(m/\t/,$_); next if ($_ =~ m/^CDS/); chomp($_); my $cds = $resutat_infos[0] ."\t"; my $reads_p1 =$resutat_infos[1]."\t"; my $reads_p2 =$resutat_infos[2]."\t"; my $count_reads_p1 =$resutat_infos[3]; my $count_reads_p2 =$resutat_infos[4]; my $count_reads_ambigu =$resutat_infos[5]; my $count_reads_classe =$resutat_infos[6]; my $reads_totaux =$resutat_infos[8]."\t"; $hash_sample_cds_RT{$cds}{$sample_name} = $reads_totaux ; $hash_sample_cds_R1{$cds}{$sample_name} = $reads_p1 ; $hash_sample_cds_R2{$cds}{$sample_name} = $reads_p2 ; $hash_read_p1_count{$cds}{$sample_name} = $count_reads_p1; $hash_read_p2_count{$cds}{$sample_name} = $count_reads_p2; foreach my $group(keys(%hash_geno_group)) { print $group."\n"; if (exists($hash_geno_group{$group}{"sample"}{$sample_name +})) { $hash_geno_group{$group}{$cds}{"ambigu"} += $count_rea +ds_ambigu; $hash_geno_group{$group}{$cds}{"rc"} += $count_reads_c +lasse; $hash_geno_group{$group}{"sample"}{$sample_name}=($cou +nt_reads_p1+$count_reads_p2); } } } close(RESULTAT); print "Finished\n"; }

my output is :

[foulquie@node6 test]$ perl test_fontion4.pl BC list_geno_palm_51_list +e_genotype test 30 1 Use of my $_ is experimental at test_fontion4.pl line 44. Use of my $_ is experimental at test_fontion4.pl line 68. keys on reference is experimental at test_fontion4.pl line 187. keys on reference is experimental at test_fontion4.pl line 211. keys on reference is experimental at test_fontion4.pl line 235. keys on reference is experimental at test_fontion4.pl line 259. $VAR1 = { 'genotype13' => { 'sample' => 'BC13_IV_.RG' }, 'genotype11' => { 'sample' => 'BC11_III_.RG' }, 'genotype14' => { 'sample' => 'BC14_III_.RG' }, 'genotype16' => { 'sample' => 'BC16_III_.RG' }, 'genotype21' => { 'sample' => 'BC21_V_.RG' }, 'genotype24' => { 'sample' => 'BC24_IV_.RG' }, 'genotype1' => { 'sample' => 'BC1_IV_.RG' }, 'genotype10' => { 'sample' => 'BC10_III_.RG' }, 'genotype23' => { 'sample' => 'BC23_III_.RG' }, 'genotype9' => { 'sample' => 'BC9_IV_.RG' }, 'genotype8' => { 'sample' => 'BC8_IV_.RG' }, 'genotype4' => { 'sample' => 'BC4_II_.RG' }, 'genotype22' => { 'sample' => 'BC22_IV_.RG' }, 'genotype20' => { 'sample' => 'BC20_IV_.RG' }, 'genotype15' => { 'sample' => 'BC15_III_.RG' }, 'genotype7' => { 'sample' => 'BC7_IV_.RG' }, 'genotype6' => { 'sample' => 'BC6_IV_.RG' }, 'genotype3' => { 'sample' => 'BC3_III_.RG' }, 'genotype12' => { 'sample' => 'BC12_IV_.RG' }, 'genotype19' => { 'sample' => 'BC19_IV_.RG' }, 'genotype17' => { 'sample' => 'BC17_V_.RG' } }; genotype13 Can't use string ("BC13_IV_.RG") as a HASH ref while "strict refs" in +use at test_fontion4.pl line 92, <RESULTAT> line 2.

my problem are the obvious error message but also my hash in the dumper should give more that one valeur for 'sample' like : 'genotype17' => { 'sample' => 'BC17_III_.RG' => 'BC17_V_.RG' but the key sample had only have the last value why ? plz help me thanks

Replies are listed 'Best First'.
Re: Can't use string as a HASH ref problem
by toolic (Bishop) on Apr 11, 2016 at 13:41 UTC
    tl;dr, but it sounds like you want each hash key to have multiple values. In that case, you want to push into a hash-of-arrays. Refer to perldsc. See also http://sscce.org
      thx push works one problem solved :)
Re: Can't use string as a HASH ref problem
by stevieb (Canon) on Apr 11, 2016 at 13:47 UTC

    Your code doesn't apparently show the line that is triggering the problem stated (line 92), so it's kind of difficult to help with that.

    Also, you didn't supply any example input, so we can't really test, but this line is probably wrong:

    $hash_geno_group{$sample_geno_group}{"sample"}=$sample_nm;

    Hash keys can only have one value, so you keep overwriting sample here. Use an array instead:

    push @{ $hash_geno_group{$sample_geno_group}{sample} }, $sample_nm;

    Also, the following are experimental warnings:

    Use of my $_ is experimental at test_fontion4.pl line 68. keys on reference is experimental at test_fontion4.pl line 187.

    If this is bound for production, fix them:

    while (defined(my $_ = <GENOTYPED>))

    to:

    while (<GENOTYPED>)

    For the keys on reference which you have no examples of in the code, you probably want something like:

    keys %{ $hash_ref }

    Use the three-arg form of open(), and use lexical file handles:

    open my $genotyped_fh, '<', $list_geno or die "Cannot open $list_geno\ +n";

      the line 92 is the line with th exists function

        Yes, but we need to see that code to identify what's going on. Preferably that line, and perhaps 5 lines before it.