Print is not in ordered after hash converted to array

Perl:

Can anyone tell me why after I convert the hash into an array, when I print it out, it's not in the order like the hash? See below..

my %cityZip = ("Logan, AL", 35098,
               "Los Angeles, CA", 90001,
               "OrangeVille, IL", 61060,
               "Palm Bay, FL", 32905,
               "Smithville, GA", 31787,
               "Haiku, HI", 96708,
               "Calder, ID", 83808,
               "Sioux City, IA", 51101,
               "Lexington, KY", 40502,
               "New Orleans, LA", 70129,
               "Newburg, MD", 20664,
               "Brooklyn, NY", 11230,
               "Las Vegas, NV", 89101,
               "Charlotte, NC", 28202,
               "Columbus, OH", 43222,
               "Garden City, SD", 57236,
               "Houston, TX", 77002,               
               "Paragonah, UT", 84760,
               "Rockport, WA", 98283,
               "Newcastle, WY", 82701);

my @zipCode = %cityZip; 
my $arraySize = @zipCode;  

my $i;
for ($i = 0; $i < $arraySize; $i+=2) {
   print "$zipCode[$i] = $zipCode[$i+1] \n";
}  

And the output I got was...

Logan, AL = 35098 
Palm Bay, FL = 32905 
New Orleans, LA = 70129 
Smithville, GA = 31787 
Houston, TX = 77002 
Garden City, SD = 57236 
Rockport, WA = 98283 
Charlotte, NC = 28202 
Los Angeles, CA = 90001 
Columbus, OH = 43222 
Newcastle, WY = 82701 
Haiku, HI = 96708 
Las Vegas, NV = 89101 
Lexington, KY = 40502 
Newburg, MD = 20664 
OrangeVille, IL = 61060 
Sioux City, IA = 51101 
Paragonah, UT = 84760 
Calder, ID = 83808 
Brooklyn, NY = 11230 

Hashes are unordered lists. The order you see the key-value pairs in the code does not apply to the actual hash. You could just print the hash instead of converting the hash to an array just to print it. You will still have the same problem with the order though.

Thanks for the reply. Well, I converted it to an array because I'm not too familiar with hash, and I need to get the data at like an index.

You could do something like this, a hash of arrays:

my %cityZip = (
   AL => ['Logan', 35098],
   CA => ['Los Angeles', 90001],
   IL => ['OrangeVille', 61060],
   FL => ['Palm Bay', 32905],
   GA => ['Smithville', 31787],
);
for my $keys (sort keys %cityZip) {
    print "$cityZip{$keys}->[0], $keys = $cityZip{$keys}->[1]\n";
}

But keep in mind that hash keys must be unique, so if you can have more than one city and zipcode per state the data structure would need to be changed a bit.

I was assuming you wanted the output in order by state, but after looking at your hash I may have been mistaken. If that is the order then the hash of arrays should work, although you may not have your head around such a concept if you are still unfamiliar with a regular old hash.

You were right. I wanted the output to be in the ordered (by the state) of the hash. The output I provided in post #1 was what I got (unordered), not what I wanted.

I tried your code, and the output wasn't in order also. So I guess you were right that hash are unordered list.

The hash I posted will not be in order but the output will because the hash keys are sorted in ASCII order in the loop:

for my $keys (sort keys %cityZip) {

Technically hashes are in order, but there is no gauranteed order. And they will not be in the same order you code them as unless that just happens by luck. Use the sort() function to sort a hash into the required order.