2d array in sub
- From: jlumley@xxxxxxxxxxxx (James)
- Date: Tue, 4 Sep 2007 15:49:35 +0000 (UTC)
I'm writing a sub that take as input, references to a 2d array and a hash-of-
arrays. The 2d array never changes but I operate on and return an updated
hash-of-arrays.
My issue is that I can get the sub working fine, iterating though the 2d array
correctly until I put a return statement in. Once I put the return statement
in, the sub only reads one slice of the 2d array, thus not returning a
correctly updated array... why??? am I missing something, surely it should
carry on operating on the whole array.
Simplified code example below. Works fine, printing results, unitl you
uncomment the "#return($pointer);" line:
#!/usr/bin/perl
@matrix=(["B1","1.00","0.80","0.33","0.34","0.03","0.05","0.01"],
["B2","0.80","1.00","0.29","0.31","0.01","0.03","0.01"],
["B3","0.33","0.29","1.00","0.73","0.41","0.28","0.31"],
["B4","0.34","0.31","0.73","1.00","0.33","0.22","0.24"],
["B5","0.03","0.01","0.41","0.33","1.00","0.43","0.46"],
["B6","0.05","0.03","0.28","0.22","0.43","1.00","0.47"],
["B7","0.01","0.30","0.24","0.46","0.47","0.47","1.00"],
);
%ctrs=( 2 => [],
4 => [],
);
$p2_header=\@header;
$p2_matrix=\@matrix;
$p2_ctrs=\%ctrs;
$duff=columns_to_clusters($p2_matrix,$p2_ctrs);
print "$duff\n";
sub columns_to_clusters
{
my $tmp_matrix=shift;
my $tmp_ctrs=shift;
my @tmp_matrix=@{$tmp_matrix};
my %tmp_ctrs=%{$tmp_ctrs};
# iterate through columns in matrix
for my $col (1 .. $#tmp_matrix) {
my $tmp_dist=999;
my $tmp_ctr='null';
# iterate through ctrs
foreach my $kno (keys %tmp_ctrs) {
print "col $col, ctr $kno => dist $tmp_matrix[$kno][$col]\n";
if ((defined $tmp_dist) && ($tmp_matrix[$kno][$col]
<$tmp_dist)) {
$tmp_dist=$tmp_matrix[$kno][$col];
$tmp_ctr=$kno;
} elsif (!defined $tmp_dist) {
$tmp_dist=$tmp_matrix[$kno][$col];
$tmp_ctr=$kno;
}
}
print "For col #$col => closest ctr is $tmp_ctr at dist $tmp_dist\n";
my @tmp=$tmp_ctrs{$tmp_ctr};
unshift(@tmp,"$col");
$tmp_ctrs{$tmp_ctr}=[@tmp];
$pointer=\%tmp_ctrs;
#return($pointer);
}
}
1;
.
- Follow-Ups:
- Re: 2d array in sub
- From: Tom Phoenix
- Re: 2d array in sub
- Prev by Date: Re: Can't locate object method
- Next by Date: Re: question about a routine
- Previous by thread: RE: Want to interactively run a shell script
- Next by thread: Re: 2d array in sub
- Index(es):
Relevant Pages
|