srkaeppler has asked for the wisdom of the Perl Monks concerning the following question:

hey monks- I am trying to write a program for a database of stars that will sorted by radial distance from the center coordinate. So let me show the code

#!/usr/local/bin/perl -w open (INPUT, "ngc188.catal") || die "died opening input\n"; open (STDOUT, ">sortedngc") || die "died opening output\n"; $n =0; while ($inline = <INPUT> ) { @firstline = split(/\s+/, $inline); ######################################### $pi = 3.141592654; $cos = cos(($firstline[3] * $pi) / 180); $x = abs(($firstline[2]* $cos) - (11.083333 * cos((85.315 * $pi) / 180 +))); $x2 = $x * $x; #squaring the x-xo term $y = abs($firstline[3] - 85.3150000); $y2 = $y * $y; #squaring the y-yo term $r2 = $y2 + $x2; #distance in decimal degree ########################variable definition########################### +## $id[$r2] = $firstline[1];#id $ra[$r2] = $firstline[2];#RA $dec[$r2] = $firstline[3];#dec $prob[$r2] = $firstline[11];#probablity $mag[$r2] = $firstline[12];#magnitude $bv[$r2] = $firstline[13];#b-v ###################################pushing######################## push (@dist, $r2); $n++; } sub numerically { $a <=> $b}; @sorted = sort numerically @dist; @nindex = (0..$n-1); foreach $i (@nindex) { print STDOUT "$id[$sorted[$i]] $ra[$sorted[$i]] $dec[$sorted[$i]] +$prob[$sorted[$i]] $mag[$sorted[$i]] $bv[$sorted[$i]] $sorted[$i]\n"; }
the problem that is happening is that when i get an output, it looks like:
9596 13.4454218 85.0299299 0 17.256 0.907 1.19274490573905e-06 9596 13.4454218 85.0299299 0 17.256 0.907 7.9866617413342e-06 9596 13.4454218 85.0299299 0 17.256 0.907 1.43567635042601e-05 9596 13.4454218 85.0299299 0 17.256 0.907 1.87711214620725e-05 9596 13.4454218 85.0299299 0 17.256 0.907 3.03218139005967e-05 9596 13.4454218 85.0299299 0 17.256 0.907 3.45815761885486e-05 9596 13.4454218 85.0299299 0 17.256 0.907 3.59984633166351e-05 9596 13.4454218 85.0299299 0 17.256 0.907 7.01615131685114e-05 9596 13.4454218 85.0299299 0 17.256 0.907 9.57233947085147e-05 9596 13.4454218 85.0299299 0 17.256 0.907 9.70028702157815e-05 9596 13.4454218 85.0299299 0 17.256 0.907 9.78283014973695e-05 9596 13.4454218 85.0299299 0 17.256 0.907 9.99000064463008e-05 . . .
the code continues to repeat in this format, where, as one will notice the last column is changing in decending order which is good. But what i would like are the values for the different variables(such as $ra, $dec) to correspond to that particular distance value($r2)(which is the last column of the output) so for example:

0043 13.35673 85.10234 0 17.256 0.245 1.19274490573905e-06 0521 13.89567 85.987636 0 12.234 0.907 7.9866617413342e-06 1000 13.235678 85.985463 0 18.314 0.907 1.43567635042601e-05 . . .
So that is what i want, is to have each different value corresponding to a particular distance(which is the last column) Now I have been wondering if perhaps i am not storing the values anywhere in the "while loop" for the program to pull from later on to eventally line things up with. the 9596 term is the last term in the loop iteration. This is why i suspect that the loop terms are not being stored anywhere, but my program is pulling from the last term of the iteration. If you can help me, that would be good. also i may need to clarify what i am saying, so if so, let me know, and ill reply with something more specific. thank you!

Replies are listed 'Best First'.
Re: sorting/output
by idsfa (Vicar) on Dec 09, 2004 at 21:36 UTC

    This method is fundamentally flawed. What happens when two stars have the same distance? Why are you calculating spherical co-ordinates with a cartesian distance? Are you aware that RA (Right Ascension) is measured in hours, (from 0-24), not degrees?

    (Updated: Yes, [id://hardburn], technically 0h to 23h 59m 59.999999...s, but I was trying to be lazy.)

    On the technical level, you are trying to index your array using a non-integer value $r2 which is much much less than one, so your calls to $foo[$sorted[$r2]] are returning $foo[$sorted[0]]. There are other stylistic issues, but this is your main technical problem.

    You would do better to use a hash, as suggested by dpuu ... though for the wrong reason. Even better would be to rotate your coordinates so that your center point is at +90, as then distance becomes simple subtraction. Perhaps Astro::Coords can help?

    use Astro::Coords; $c = new Astro::Coords( name => "My center", ra => '05:22:56', dec => '-26:20:40.4', type => 'B1950' units=> 'sexagesimal'); $t = new Astro::Coords( name => "My target", ra => '05:22:56', dec => '26:20:40.4', type => 'B1950' units=> 'sexagesimal'); print $c->distance($t);

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
Re: sorting/output
by dpuu (Chaplain) on Dec 09, 2004 at 21:25 UTC
    I found the code a bit confusing, but I think what is happenning is that you are creating an array that maps a distance from the centre to a record -- but you don't account for the possibility of two records with the same distance.

    The approach you probably want to use is to create a reshref for each record, and then sort those by distance. i.e.

    while (<LINE>) { @fields = split; my $record = { ra => ..., prob => ..., dist => ..., }; push @records, $record; } sub by_distance { $a->{dist} <=> $b->{dist} } @records = sort by_distance @records;

    You can then iterate ocver the records, and print them.

    --Dave
    Opinions my own; statements of fact may be in error.