Strange Problem

  • Thread starter rajasekaran.natarajan
  • Start date
R

rajasekaran.natarajan

the code is a part of a large program, in which everythign works fine
except the IF BLOCK why? whats wrong with that. I tried everything I
can, yesterday. But no use. I do not know debugging But I have inserted
print commands to check what is going on. only IF block is not working.
WHY?

1. It is executing with a message saying

MESSG:use of Uninitiallized value (refering to $cn[$s1]) at line no#

but it can not be becuase the intial shortest is very high and the all
calculated $dist is less than 30. It *HAS* to execute the IF block and
assign the $cn[$s1] some value.

2. All calculated $dist are less than 30. but when I see the output it
is printing
1,,10000
2,,10000
3,,10000
it says it is not assigning $dist to $shortest by IF loop WHY? WHY?
WHY?

my $shortest=100000;
#assiging initial values#
foreach my $s1 (@set1){
#from array set1, the sample point is s1#
foreach my $s2 (@set2) {
#from array set2 the sample point is s2#
($x1,$y1,$z1) = ($g{$s1}->{x},$g{$s1}->{y},$g{$s1}->{z});
#this picks the coordinates for point s1 from hash#
($x2,$y2,$z2) = ($g{$s2}->{x},$g{$s2}->{y},$g{$s2}->{z});
#this picks the coordinates for point s2 froma hash #
my $dist= sqrt(($x1-$x2)**2+($y1-$y2)**2+($z1-$z2)**2);
#thiscalculate dist bet s1 and s2 #
if($dist le $shortest){
# if the dist is .LE. the shortest then do this #
$shortest = $dist;
#assign the calculated dist as the new shortest#
$cn[$s1] = $s2;
# store this particular point s2 in cn#
}
}
print OUT "$s1,$cn[s1],$shortest\n";
# print s1 and closest point s2 then the distance #
$shortest = 100000; #initialize for next iteration #
}
 
G

Gary E. Ansok

my $shortest=100000;
if($dist le $shortest){

The "le" operator operates as if its arguments are strings --
and when compared as strings, "100000" is less than practically
any value you're likely to compute (the only ones that will compare
less are "10000", "1000", etc.)

What you probably want is

if($dist <= $shortest){

-- Gary
 
X

xhoster

my $dist= sqrt(($x1-$x2)**2+($y1-$y2)**2+($z1-$z2)**2);
#thiscalculate dist bet s1 and s2 #
if($dist le $shortest){

You appear to be using string comparison on numeric values.

if ($dist <= $shortest) {

Xho
 
R

rajasekaran.natarajan

Thanks Ansok and Xho for your replies,
you dont beleive it I spend full afternoon wondering what is going on!
today morning I can posted it here and went back to have a look at it.
Then I found out accidently what was the problem!

The book I have said this clearly like ..
le is the string equivalent for <=
(is it string equivalent or equivalent operator for strings ;-)

I think I need to improve my english.

thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top