Title: Re: hash values are printed in reverse order
That's pretty easy to answer: hashes aren't sorted!
If you want them sorted, you either have to use

    foreach $key ( sort keys %hash )

Or if they are to be kept in the order in which they were defined (ie: chronological), you need to keep the keys in a separate array for sorting purposes:

    foreach $key ( @key_list )
        {
        print "$key: $hash{$key}\n" if exists $hash{$key};
        }

See man perldata or perldsc for more information than you ever wanted to know.


Steve


On 22.09.2005 19:59 Uhr, "neelay thaker" wrote:

Hi,
when I parse a list of comma separated IP addresses, put them into an array and then transfer the elements of the array into a hash, the hash elements get printed in reverse order as compared to the array and the input. The following code and output will make this problem clear.
The code snipet is this-
                $arrsize = @ipaddr;
               for( $ctr = 0 ; $ctr < $arrsize ; $ctr++ )
                {
                       $ip_intf_hash{ $ipaddr[$ctr] } = "";
                       print $ipaddr[$ctr];
                       print " : ";
                }
                print "\n";
               print "You entered the IPaddresses:\n";
                foreach $nfr ( keys %ip_intf_hash )
                {
                       print "$nfr : ";
                }
                print "\n";

The output looks like this-
              Enter the IP addresses: 2.2.2.2 <http://2.2.2.2> ,3.3.3.3 <http://3.3.3.3> ,1.1.1.1 <http://1.1.1.1>
               2.2.2.2 <http://2.2.2.2>  : 3.3.3.3 <http://3.3.3.3>  : 1.1.1.1 <http://1.1.1.1>  :
              You entered the IP addresses:
              1.1.1.1 <http://1.1.1.1>  : 3.3.3.3 <http://3.3.3.3>  : 2.2.2.2 <http://2.2.2.2>  :
Any ideas on why the hash elements get reversed?
Thanks.

Neelay.


Der Inhalt dieser E-Mail oder eventueller Anhange ist ausschliesslich fur den bezeichneten Adressaten bestimmt. Wenn Sie nicht der vorgesehene Adressat dieser E-Mail oder dessen Vertreter sein sollten, so beachten Sie bitte, dass jede Form der Kenntnisnahme, Veroffentlichung, Vervielfaltigung oder Weitergabe des Inhalts dieser E-Mail unzulassig ist. Wir bitten Sie, sich in diesem Fall mit dem Absender der E-Mail in Verbindung zu setzen.

The information contained in this email is intended solely for the addressee. If you are not the intended recipient, any form
of disclosure, reproduction, distribution or any action taken or refrained from in reliance on it, is prohibited and may
be unlawful. Please notify the sender immediately.

Reply via email to