Reputation: 121
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);
}
}
Upvotes: 1
Views: 96
Reputation: 15121
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.
Upvotes: 2