PDA

View Full Version : Beginner Question About References


big_cheese
07-28-2006, 03:06 AM
Hi,

Can I pass references of hashes (like \%h) to subroutines and have the subroutine create new keys and values for the hash (%h)? From the documentation it seems possible, but I cannot do it.

This is a summary of my Code

The &read subroutine reads a data file "GC2.lis" and creates new keys values for three hashes


&read("GC2.lis", \%nom, \%slow, \%fast);

sub read {
die "Cannot open file '$_[0]': $!" unless (open READ, $_[0]);

foreach $i (2..4) {
LOOP: while (<READ>) {

if (/^\s+(\S+)\s+(\S+)\s*$/) {

${$_[$i]}{$1} = $2;
}
elsif (/^y$/) {
last LOOP;
}

}

}
close READ;
return;
}


Thanks in advance for your help. Glad I could join.

big_cheese
07-28-2006, 07:06 AM
Ok 12 hours later I think found the problem, foreach $i (2..4) should be foreach $i (1..3), arrays indices start from 0 not 1.

KevinADC
07-28-2006, 08:08 PM
arrays indices start from 0

yes, and you have that in your code: $_[0]

big_cheese
07-28-2006, 10:24 PM
yes, and you have that in your code: $_[0]

True, but then I jumped to $_[2] instead of $_[1], so I wrote:

foreach $i (2..4)

instead of :

foreach $i (1..3)

Thanks guys. More questions to come I'm sure :)