1

I am trying to count how many time DE10 and each of the keys in my ICD10 hash occurs in the same line in my file2.tsv. I further have to divide it by male/female (M/K). I therefore made a hash called results. Each of the keys in this is named after the key in the ICD10 hash, and they refers to an array of 2 elements, the first counting the male, the second counting the females.

But I get this warning:

Can't use string ("0") as an ARRAY ref while "strict refs"

due to this line:

  $results{$key}[1] +=1;

I am a little weak on this reference part, can someone help me with my mistake? thanks a lot

#!/usr/bin/perl -w
use strict;

###################
# loading my hash #
###################

my %icd10;

open(IN, '<', 'myfile.tsv') or die;
while (defined (my $line = <IN>)) {
chomp $line;
$icd10{$line} = 1;
}
close IN;

################
### COUNTING
#################

my %results;

open(IN, '<', 'myfile2.tsv') or die;
while (defined (my $line = <IN>)) {
chomp $line;
my @line = split('\t', $line);
my %hash;
for (my $i = 2; $i < scalar(@line); $i++){ 
    $hash{$line[$i]} = 1;
}

if (grep (m/^DE10/, keys %hash)) {
    foreach my $key (keys %icd10){
        if (grep (m/^$key/, keys %hash)) {
            if (exists $results{$key}) {
                if ($line[1] eq 'M') {
                    $results{$key}[1] +=1;
                }
                elsif ($line[1] eq 'K'){
                    $results{$key}[2] +=1;
                }
            }
            else{
                if ($line[1] eq 'M') {
                    $results{$key}=(1,0);
                }
                elsif ($line[1] eq 'K'){
                    $results{$key}=(0,1);
                }
            }
5
  • The parentheses in these two sentences $results{$key}=(1,0); and $results{$key}=(0,1); should be brackets, if you want $results{$key} to be a reference to an array. Commented Jan 18, 2014 at 10:08
  • Is this line a typo if (grep (m/^DE10, keys %hash)) { ? It should be if (grep (/^DE10/, keys %hash)) { Commented Jan 18, 2014 at 10:09
  • M42: sorry, that was a copy paste error. Commented Jan 18, 2014 at 10:11
  • leeduhem: so it should be $results{$key}={1,0} ? It doesnt fix the problem Commented Jan 18, 2014 at 10:13
  • It should be $results{$key} = [1,0]; Commented Jan 18, 2014 at 10:20

1 Answer 1

2

If you want $results{$key} to be a reference to an array, then the parentheses in these two identical sentences $results{$key}=(1,0); should be square brackets, like this: $results{$key}=[1,0];.

To create a reference to an array, you can use backslash operator:

$arrayref = \@array;

To create a reference to an anonymous array you should use square brackets:

$arrayref = [ 'ele1', 'ele2' ];

See perlref for further details.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I might be really stupid , but Im not sure what I should change. if I write $results{$key}=[1,0]; I still have the same problem.
Im not sure what you mean by brackets
@user2335015 Change both $results{$key}=(1,0); to $results{$key}=[1,0]; does not work?
Arh sorry in my real code, there were a couple more places I need to change it. It seems to be running now. Thanks :)
@user2335015 Already changed 'brackets' to 'square brackets', I hope the meaning of this one is clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.