Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Can't use string ("0") as a HASH ref while "strict refs" problem

by etricaen (Novice)
on Apr 12, 2016 at 09:36 UTC ( [id://1160205]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks i have a "Can't use string ("0") as a HASH ref while "strict refs" in use at test_fontion4.pl line 117." this line is the "foreach my $RC(values %{$hash_geno_group{$sample_geno_group}{$cds}{"rc"}})" line , $RC has to be a number this is a art of my code :

#creation de la matrice des genotype selon les groupes genotypique foreach my $sample_geno_group(keys(%hash_geno_group)) { print $sample_geno_group. "\n"; foreach my $cds (keys %{$hash_geno_group{$sample_geno_group}}) { print "cds ".$cds. "\n"; next if ($cds eq "sample"); foreach my $RC(values %{$hash_geno_group{$sample_geno_group}{$ +cds}{"rc"}}) { print "rc ".$RC. "\n"; #verification du nombre minimun de reads pour chaque group +e genotypique if ($RC < $seuil_min_classe) { foreach my $ech_name(values @{$hash_geno_group{$sample +_geno_group}{"sample"}}) { $hash_sample_cds_genotype{$cds}{$ech_name} = "NA\t +" ; } delete($hash_geno_group{$sample_geno_group}); } else { foreach my $AB(values%{$hash_geno_group{$sample_geno_g +roup}{$cds}{"ambigu"}}) { #verification du pourcentage maximun de reads ambi +gu pour chaque groupe genotypique if (($AB *100 / $RC) > $seuil_max_amb) { foreach my $ech_name(values @{$hash_geno_group +{$sample_geno_group}{"sample"}}) { $hash_sample_cds_genotype{$cds}{$ech_name} + = "NA\t" ; } delete($hash_geno_group{$sample_geno_group}); } } } }

how can i fix thanks

Replies are listed 'Best First'.
Re: Can't use string ("0") as a HASH ref while "strict refs" problem
by Corion (Patriarch) on Apr 12, 2016 at 09:43 UTC

    You have not shown us a small, self-contained example that reproduces the problem.

    Most likely, the problem is that your datastructure is not what you think it is, and/or it doesn't match the way your code tries to use it. In the code path leading up to the problematic line, you output the variables $RC and< c>$cds</c> and others. Maybe you can share the values that you see there with us.

    Also very good for visualizing a data structure is the module Data::Dumper. Print out what Perl has as your data structure using

    use Data::Dumper; print Dumper \%hash_geno_group;

    If the output from Data::Dumper is too large, reduce the data you store into %hash_geno_group and store only the data where the problem happens.

      this a output of part of the dumper :

       print Dumper(\%hash_geno_group{$sample_geno_group});

      after :

       next if ($cds eq "sample");

      this my output :

      'XM_010928396.1 ' => { 'ambigu' => 156, 'BC11_III_.RG' => 75, 'rc' => 195, 'BC11_II_.RG' => 120 }, 'XM_010930511.1 ' => { 'BC11_II_.RG' => 258, 'ambigu' => 4, 'rc' => 1074, 'BC11_III_.RG' => 816 }, 'XM_010922249.1 ' => { 'BC11_II_.RG' => 0, 'ambigu' => 0, 'BC11_III_.RG' => 0, 'rc' => 0 }, 'XM_010912985.1 ' => { 'BC11_II_.RG' => 0, 'rc' => 0, 'ambigu' => 0 }, 'XM_010935705.1 ' => { 'BC11_II_.RG' => 0, 'ambigu' => 0, 'rc' => 0, 'BC11_III_.RG' => 0 }, 'XM_010939661.1 ' => { 'ambigu' => 0, 'BC11_III_.RG' => 0, 'rc' => 0, 'BC11_II_.RG' => 0 }, 'XM_010916522.1 ' => { 'BC11_II_.RG' => 0, 'rc' => 0, 'BC11_III_.RG' => 0, 'ambigu' => 0 }, 'XM_010930873.1 ' => { 'BC11_II_.RG' => 0, 'ambigu' => 0, 'rc' => 0, 'BC11_III_.RG' => 0 }, 'XM_010926039.1 ' => { 'BC11_II_.RG' => 0, 'BC11_III_.RG' => 0, 'rc' => 0, 'ambigu' => 0 }, 'XM_010928394.1 ' => { 'BC11_II_.RG' => 0, 'ambigu' => 0, 'BC11_III_.RG' => 0, 'rc' => 0 } }; Can't use string ("75") as a HASH ref while "strict refs" in use at te +st_fontion4.pl line 118.

      can you see where is my problem ?

        Perl already tells you where your problem is. In line 118.

        You haven't shown us the values of $cds or $RC, and you don't show us the line 118 from your script.

        From the error message Can't use string ("75") as a HASH ref and from the data you've showed, I can guess that your script tries to use the 75 from this hash entry:

        'XM_010928396.1 ' => { 'ambigu' => 156, 'BC11_III_.RG' => 75, 'rc' => 195, 'BC11_II_.RG' => 120 },

        Maybe you want to rerun your script using only this data, and then you should easily find out where your problem is.

        Maybe you want to reduce your deep diving through the data structure into several steps, giving Perl the chance to be more to the point with its error messages:

        # Convert this: values %{$hash_geno_group{$sample_geno_group}{$cds}{"rc"}} # to this: my $sample_geno_group_ref = $hash_geno_group{$sample_geno_group}; my $sample_cds = $sample_geno_group_ref->{$cds}; my $sample_rc = $sample_cds->{"rc"};

        Maybe also output the Dumper of each step so you can see where you are within your data structure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1160205]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found