Hello Monks,

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!

In reply to references - proper use of infix (arrow) and deprecated hash references by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.