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

I have 300+ objects with specific values for x,y,z coordinates, and I want to determine the distance between all 300+ objects and construct a matrix in the following fashion-

A B C A 0 10 3 B 4 0 7 C 4 1 0

where the values in the matrix are the distances between A and B, A and C etc. (I just made up the numbers). I've been given data like so,

s x y z A C 79.620 54.720 53.034 B C 81.822 54.071 50.027 C C 83.871 51.966 52.424 ...

And this equation for distance:

$d = sqrt(($rx1 - $rx2)**2 + ($ry1 - $ry2)**2 + ($rz1 - $rz2)**2)

s is a shape parameter that doesn't do anything for this calculation. I realize that I need to work with loops in order to do these crazy number of permutations, but I am not sure how to begin. I tried the following way to get a code to read and store the values in the file, but I could only get it read the first line.

open(IN, "template.xyz") or die; chomp($line = < IN >); ($b, @xyz) = split /\s+/, $line; while (chomp($line = < IN >)) { ($bb, @xyzvals) = split /\s+/, $line; for ($i=0; $i<@xyzvals; $i++) { $s{$bb}{$xyz[$i]} = $xyzvals[$i]; } } close(IN);

Any advice on reading/storing the values properly and how to go about doing the permutations?

Original content restored above by GrandFather

I have 300+ objects with specific values for certain coordinates, and I want to determine a value between all 300+ objects and construct a matrix. Any advice on reading/storing the values properly and how to go about doing the permutations? (question resolved)

Replies are listed 'Best First'.
Re: Employing loops and constructing a matrix
by NetWallah (Canon) on Apr 24, 2012 at 05:07 UTC
    Try to get your data into a convenient structure - i'd suggest an array-of-hashrefs.

    Your results should be a 2-D array.

    Here is some code to get you started (populated with sample data). You need to figure out how to get your data into this structure:

    use strict; use warnings; my @points = ( {X=>79.620, Y=>66, Z=>12.3}, {X=>66.4, Y=>77, Z=>43.1}, {X=>73.6, Y=>55, Z=>93.7} ); my @results; for my $x(0..$#points){ my $p1 = $points[$x]; for my $y (0..$#points){ my $p2 = $points[$y]; my $dist = ( $p1->{X} - $p2->{X} ) **2 + ( $p1->{Y} - $p2->{Y} ) **2 + ( $p1->{Z} - $p2->{Z} ) **2 ; $results[$x][$y] = $dist; } } #------------------- for my $r (@results){ printf "%3.2f\t",$_ for @$r; print "\n"; }
    Yes - there is room for optimization, since distances are symmetric, but, at this point, operational and simple code trumps efficient code that introduces complexity.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: Employing loops and constructing a matrix
by jwkrahn (Abbot) on Apr 24, 2012 at 05:50 UTC
    chomp($line = < IN >);

    <> represents two different operators, readline or glob.    When you have whitespace it becomes the glob operator so you have:

    chomp($line = glob 'IN' );

    Or simply:

    chomp($line = 'IN' );


    for ($i=0; $i<@xyzvals; $i++) { $s{$bb}{$xyz[$i]} = $xyzvals[$i]; }

    In perl that is usually written as:

    for my $i ( 0 .. $#xyzvals ) { $s{ $bb }{ $xyz[ $i ] } = $xyzvals[ $i ]; }

    Or you could do the same thing with a hash slice:

    @{ $s{ $bb } }{ @xyz[ 0 .. $#xyzvals ] } = @xyzvals;