Re: hash values are printed in reverse order

[email protected] (Riehm Stephen) Fri, 23 Sep 2005 12:57:53 +0200
Newsgroups perl.scripts
Message-ID <BF59AC51.1115F%[email protected]>
--B_3210325074_1331275
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

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 =3D @ipaddr;
>                for( $ctr =3D 0 ; $ctr < $arrsize ; $ctr++ )
>                 {
>                        $ip_intf_hash{ $ipaddr[$ctr] } =3D "";
>                        print $ipaddr[$ctr];
>                        print " : ";
>                 }
>                 print "\n";
>                print "You entered the IPaddresses:\n";
>                 foreach $nfr ( keys %ip_intf_hash )
>                 {
>                        print "$nfr : ";
>                 }
>                 print "\n";
>=20
> 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.
>=20
> Neelay.
>=20



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..



--B_3210325074_1331275
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<HTML>
<HEAD>
<TITLE>Re: hash values are printed in reverse order</TITLE>
</HEAD>
<BODY>
<FONT FACE=3D"Times New Roman"><SPAN STYLE=3D'font-size:14.0px'>That's =
pretty easy to answer: hashes aren't sorted!<BR>
If you want them sorted, you either have to use<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;foreach $key ( sort keys %hash )<BR>
<BR>
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:<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;foreach $key ( @key_list )<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$key: =
$hash{$key}\n&quot; if exists $hash{$key};<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
<BR>
See man perldata or perldsc for more information than you ever wanted to =
know.<BR>
<BR>
<BR>
Steve<BR>
<BR>
<BR>
On 22.09.2005 19:59 Uhr, &quot;neelay thaker&quot; wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE=3D"Times New Roman"><SPAN =
STYLE=3D'font-size:14.0px'>Hi,<BR>
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.<BR>
The code snipet is this-<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;$arrsize =3D @ipaddr;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;for( $ctr =3D 0 ; $ctr &lt; $arrsize ; $ctr++ )<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ip_intf=
_hash{ $ipaddr[$ctr] } =3D &quot;&quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print =
$ipaddr[$ctr];<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print =
&quot; : &quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n&quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;print &quot;You entered the IPaddresses:\n&quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;foreach $nfr ( keys %ip_intf_hash )<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print =
&quot;$nfr : &quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n&quot;;<BR>
<BR>
The output looks like this-<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;Enter the IP addresses: 2.2.2.2 <a =
href=3D"http://2.2.2.2">&lt;http://2.2.2.2&gt;</a> ,3.3.3.3 <a =
href=3D"http://3.3.3.3">&lt;http://3.3.3.3&gt;</a> ,1.1.1.1 <a =
href=3D"http://1.1.1.1">&lt;http://1.1.1.1&gt;</a> <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;2.2.2.2 <a =
href=3D"http://2.2.2.2">&lt;http://2.2.2.2&gt;</a> &nbsp;: 3.3.3.3 <a =
href=3D"http://3.3.3.3">&lt;http://3.3.3.3&gt;</a> &nbsp;: 1.1.1.1 <a =
href=3D"http://1.1.1.1">&lt;http://1.1.1.1&gt;</a> &nbsp;:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;You entered the IP addresses:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;1.1.1.1 <a href=3D"http://1.1.1.1">&lt;http://1.1.1.1&gt;</a> =
&nbsp;: 3.3.3.3 <a href=3D"http://3.3.3.3">&lt;http://3.3.3.3&gt;</a> =
&nbsp;: 2.2.2.2 <a href=3D"http://2.2.2.2">&lt;http://2.2.2.2&gt;</a> =
&nbsp;:<BR>
Any ideas on why the hash elements get reversed?<BR>
Thanks.<BR>
<BR>
Neelay.<BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE=3D"Times New Roman"><SPAN =
STYLE=3D'font-size:14.0px'><BR>
</SPAN></FONT>
</BODY>
<!--[object_id=3D#hse24.de#]--><P align=3Dleft><FONT face=3DTahoma =
size=3D2><FONT color=3D#0000ff>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.</FONT></FONT></P>
<P align=3Dleft><FONT face=3DTahoma size=3D2><FONT color=3D#0000ff>The =
information contained in this email is intended solely for the =
addressee. If you are not the intended recipient, any form<BR>of =
disclosure, reproduction, distribution or any action taken or refrained =
from in reliance on it, is prohibited and may<BR>be unlawful. Please =
notify the sender immediately.</FONT></FONT></P></HTML>


--B_3210325074_1331275--