Strange Problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.

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
it says it is not assigning $dist to $shortest by IF loop 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->,$g->,$g->);
  #this picks the coordinates for point s1 from hash#
  ($x2,$y2,$z2) = ($g->,$g->,$g->);
  #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 #

Re: Strange Problem

>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

Re: Strange Problem wrote:

>   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) {


-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB

Re: Strange Problem

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

Site Timeline