I'm currently working on a script which takes a file as an input. The input file looks like this:
ECHANTILLON GENOTYPE
CAN1 genotype1
CAN2 genotype1
CAN3 genotype1
EUG1 genotype2
EUG2 genotype2
EUG3 genotype2
EUG4 genotype2
What I want to do is to create a hash with:
- a first key as the GENOTYPE column and
- a second key called "sample" which would point to the "ECHANTILLON" column. Its value would thus give for example CAN1, CAN2...
Here is what my script looks like:
#!/usr/local/perl-5.24.0/bin/perl
use warnings;
use strict;
use Data::Dumper;
use feature qw{ say };
use Getopt::Long;
my $list_geno;
GetOptions("g|input=s"=>\$list_geno);
my %hash_geno_group;
open(GENOTYPED,"<$list_geno") or die ("Cannot open $list_geno\n");
while (defined(my $l1= <GENOTYPED>))
{
my @geno_group_infos = split(m/\t/,$l1);
next if ($l1 =~ m/^ECHANTILLON/);
chomp($l1);
my $sample_nm = $geno_group_infos[0];
my $sample_geno_group = $geno_group_infos[1];
push @{ $hash_geno_group{$sample_geno_group}{"sample"} },$sample_nm;
foreach $sample_geno_group (keys (%hash_geno_group)){
foreach $sample_nm (values %{$hash_geno_group{%{$sample_geno_group}{"sample"}}}){
print $sample_geno_group, "\t" , $sample_nm, "\n";
}
}
}
close(GENOTYPED);
exit;
I tried to check what returns the print of $sample_nm variable but it returns me as an error
Can't use string ("genotype1") as a HASH ref while "strict refs" in use at Test.pl line 27, "GENOTYPED" line 2".
Can anybody explain me please:
- Why I do have this type or error;
- How to get values from the 1st column. I'll need further to store them into another variable in order to compare them with the same values but from another input file. Thanks !