Others have noted that perl does not guarantee the order of hash elements. There is, though a simpler way to get what you want, without changing your data structures: use hash slices to extract the values from the hash, instead of using the
values function.
e.g:
my @keys = keys %{$database[0]}; #optionally use sort keys
my $pdl1 = pdl (@{$database[0]}{@keys});
my $n1 = norm $pdl1;
my ($pdl2, $n2, $dotproduct, $d, @angles);
for (0..$3database) {
$pdl2 = pdl ((@{$database[$_]}{@keys});
$n2 = norm $pdl2;
$dotproduct = inner ($n1, $n2);
$d = dotproduct->sclr();
$angles[$_] = (180/3.1415926)*acos($d);
}
A hash slice allows you to extract many elements of a hash at a time, returning an array, in the order of the array you passed in. By passing the same keys array to each hash, you guarantee they will come out in the same order. If you want to force the order, you can sort the
@keys array however you deem appropriate...
The syntax of hash slices is a little tricky, especially when dealing with references, but you can piece it apart, or what I did is "build it up" from first principles, starting from
@hash{@keys} and going from there to
@$hashref{@keys} to
@{$arrayofhashrefs[$arrayindex]}{@keys}.
Slices (of both hashes an arrays) can give incredible expressive power. They also are more efficient than looping yourself, since the looping is done in compiled C by the perl engine, rather than in interpreted code...
Hope this helps.
--JAS
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.