Hi Brethren,

Keeping the initial order of insertion into the hash throughout the various map processes of the program. Below illustrates the various stages. I would like the insertion order of %hashA “key” to template the order of %hashB “value”. Then the order of %hashB “key” used to template the order of %hashC “key”. The actual program populates the hashes from a text files. hashA insertion order is descending order by value, hashB, and hashC are in ascending order by key. I had not appreciated that this order would not be retained as perl appears to default sort by key alpha numerically.
It has been suggested that Tie::hash could be an answer, there seems to be many different versions, perhaps someone with experience of Tie::hash could point me in the right direction?

UPDATE
The naming of some of the arrays may have been inappropriate as they may convey that they have been sorted when in fact they have not as pointed out by "limal" also copy and paste error in desired output
Thanks to all in advance.
Gavin
#!usr/bin/perl -W use strict;# forces predeclaration of variables use diagnostics;#diagnostic tools #use Data::Dumper::Simple;#print out contents of variables use porter; # load the porter stemmer module my %hashA = ( 'accord newton second' => 80, 'acceler smaller greater' => 78, 'addit law motion' => 56, 'meant bodi act forc'=> 55, ); foreach my $key(keys %hashA){ #print "$key $hashA{$key}\n"; } my %hashB = (2 => 'addit law motion', 3 => 'accord newton second', 4 => 'meant bodi act forc', 5 => 'acceler smaller greater', ); foreach my $key(keys %hashB){ #print "$key $hashB{$key}\n"; } my @sorted_scores = keys %hashA;#keys from hash A foreach (@sorted_scores){# this gives sorted text #print "$_\n"; } my @sorted_words = map { $hashA{$_} } @sorted_scores;#sorted text foreach (@sorted_words){ #print "$_\n";# this gives the scores sorted out } my %hashC = (2 => "In addition to ", 3 => "According to Newton", 4 => "It also meant that whenever ", 5 => "The acceleration is", ); my (@line_Numbers); my %reversehashB = reverse %hashB; @line_Numbers = map { $reversehashB {$_} }@sorted_scores; foreach (@line_Numbers){ #print "$_\n"; } foreach (@line_Numbers){ print ($_,' ', $hashC{$_}, "\n"); }
The actual out put and desired out put are shown beow
Actual Data Out 4 It also meant that whenever 5 The acceleration is 3 According to Newton 2 In addition to Desired Data Out 3 According to Newton 5 The acceleration is 2 In addition to 4 It also meant that whenever

In reply to Retaining Insertion order throughout when using “map” by Gavin

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.