Anonymous Monk,
I think your distance formula is wrong. I believe you are supposed to take the square root of the sum of the deltas squared. In any account, here is working code.
#!/usr/bin/perl use strict; use warnings; my @helix; while ( <DATA> ) { chomp; next if /^\s*$/; if (/^([^:]+):/) { # Start of new HELIX definition push @helix, { name => $1 }; # Anonymous hash with 1 element ( +name) next; } # Atom definition if (/^\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*/) + { push @{$helix[-1]{atom}}, {name => $1, id => $2, x => $3, y => + $4, z => $5 }; } } # Go through @helix in order for my $i (0 .. $#helix - 1) { print "Comparing helix $helix[$i]{name}\n"; my @atom_1 = sort { $a->{id} <=> $b->{id} } @{$helix[$i]{atom}}; for my $j ($i + 1 .. $#helix) { print "\tAgainst helix $helix[$j]{name}\n"; my @atom_2 = sort { $a->{id} <=> $b->{id} } @{$helix[$j]{atom} +}; for my $a1 (@atom_1) { for my $a2 (@atom_2) { my $distance = distance($a1, $a2); print "\t\tDistance between $a1->{name} and $a2->{name +} is $distance\n"; } } } print "\n\n"; } sub distance { my ($a1, $a2) = @_; my $delta_x = ($a1->{x} - $a2->{x}) ** 2; my $delta_y = ($a1->{y} - $a2->{y}) ** 2; my $delta_z = ($a1->{z} - $a2->{z}) ** 2; return sqrt($delta_x + $delta_y + $delta_z); } __DATA__ HELIX1: x y z A 1 -1.115 8.537 7.075 B 2 -2.745 5.280 7.165 C 3 -0.777 3.267 7.329 D 4 1.610 5.225 10.885 E 5 0.296 5.263 10.912 HELIX2: K 1 -0.696 13.041 22.357 L 2 1.152 11.081 23.082 M 3 2.200 17.590 18.424

It uses an AoHoAoH. Since you haven't indicated if this is a programming assignment within your biology course I haven't commented the code. It is more complicated then it needs to be but that is so that it can easily be adapted to meet unstated requirements. Please ask questions if you don't understand.

Cheers - L~R


In reply to Re: confused with distances by Limbic~Region
in thread confused with distances by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.