Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I just learned how to use References, but I am still confused. Currently, I try to write a script that reads a txt file that has 5 columns, each separated by some whitespace. Each Row corresponds to a gene and the columns are the 1. Gene identifyer 2. sequence 3. sequence length 4. GC content 5. CAI. It's not important what that is.
I want to store the individual attributes into a hash with the identifyer as keys, and a reference to an array containing the remaining attributes as the values. When I try to print the hash, perl complains about my infix operator, except when I write the % symbol instead of the $ symbol. But I learned it the other way. In addition, Perl complains that hash references are deprecated. I'm confused.
Here's my code:my $file = 'data.txt'; open READ, $file || die "Cannot open $file: $!\n"; my %hash; while (<READ>){ chomp; my ($id, $seq, $length, $gc, $cai) = split /\s+/, $_; $hash{$id} = [ $seq, $length, $gc, $cai ]; } close (READ); foreach (keys %hash){ print $_, "\t", %hash->{$_}[1], "\t", %hash->{$_}[2], "\n"; }
this code only leads to the warning: Using a hash as a reference is deprecated! When I change the last line to print $_, "\t", $hash->{$_}[1], ... it also complains: Global symbol $hash requires explicit ...
Can anyone clarify the situation? Thanks in advance!
|
|---|