Print is not in ordered after hash converted to array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print is not in ordered after hash converted to array
# 1  
Old 04-07-2009
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..

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


Last edited by teiji; 04-07-2009 at 11:56 PM..
# 2  
Old 04-08-2009
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.
# 3  
Old 04-08-2009
Quote:
Originally Posted by KevinADC
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.
# 4  
Old 04-08-2009
You could do something like this, a hash of arrays:

Code:
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.
# 5  
Old 04-08-2009
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.
# 6  
Old 04-10-2009
Quote:
Originally Posted by KevinADC
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.
# 7  
Old 04-10-2009
Quote:
Originally Posted by teiji
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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to print hash of hash in table format

Hi, I have a hash of hash where it has name, activities and count i have data like this - $result->{$name}->{$activities} = $value; content of that are - name - robert tom cat peter activities - running, eating, sleeping , drinking, work i need to print output as below ... (3 Replies)
Discussion started by: asak
3 Replies

2. Programming

Perl Array within an hash

Hi All I have been using a curl code to output an hash that looks like this $VAR1 = { 'data'... (5 Replies)
Discussion started by: ab52
5 Replies

3. Shell Programming and Scripting

array of hash - perl

How do I get the unique hashes from an array of hashes? @ar1 = ( {a=>1,b=>2}, {c=>3,d=>4},{a=>1,b=>2});I need : @ar2 = ( {a=>1,b=>2}, {c=>3,d=>4});Thanks. (2 Replies)
Discussion started by: shellwell
2 Replies

4. Web Development

Intersection and union of array by hash

Hi, A piece of script from Perl-cookbook I do not understand, and post here for explanation. The purpose is to find the element in either array (union), and in both array (intersection). Thank you in advance. @a=qw(1 3 5 6 7 8); @b=qw(2 3 5 7 9); foreach $e (@a, @b) {$union{$e}++ &&... (3 Replies)
Discussion started by: yifangt
3 Replies

5. UNIX for Dummies Questions & Answers

Print folder size ordered by pattern value of specific file type :-) ! challenge !

Hello dear unix command line friends ! I'm looking for a simple combinaison of ls & awk (maybe grep) to print: list of folders of a directory |_ ordered by size like what I have with $ du -sk ./* | sort -rn printing that result: 8651520 ./New Virtual Machine_1 8389120 ./Redhat ... (1 Reply)
Discussion started by: holister
1 Replies

6. Shell Programming and Scripting

Array of hash in perl does not work

Hi , I have an input.txt file that i read node: id= c1, class=nb, cx=100, cy=100, r=10 node: id=c2, class=b, cx=150, cy=130, r=10 node: id=c3, class=nb, cx=50, cy=80, r=10 node: id=c4, class=nb, cx=120, cy=200, r=10 i split over , and = to create a global array and then passed it to a... (6 Replies)
Discussion started by: rsanjay
6 Replies

7. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

8. Shell Programming and Scripting

Read csv into Hash array?

Hi all experts, May I know how to read a csv file and read the content in a hash in PERL? Currently, I hard-coded and defined it in my code. I wanna know how to make up the %mymap hash thru reading the cfg.txt ==== csv file(cfg.txt): 888,444 999,333 === #!/usr/bin/perl my... (1 Reply)
Discussion started by: kinmak
1 Replies

9. Shell Programming and Scripting

hash,array and perl

Hi,i have a code fragment below. %tag = (); #line 1 $tag{'info'} = $datastring; #line 2 $resp = $ua->request( #$ua is a user agent POST 'http://10.2.3.0' , Content_Type => application/x-www-form-urlencoded Content => #line 3 I am not sure of what the code... (3 Replies)
Discussion started by: new2ss
3 Replies

10. Shell Programming and Scripting

Hash within array, within hash, within array...

I have a little problem. To keep a configuration simple, I've exceeded my perl knowledge. :-) I've worked with multi-dimentional arrays before, but this one has me beat: @info = ( { 'defval' => 'abc' 'stats' = ( { 'name' => 'a', }, { 'name' =>... (1 Reply)
Discussion started by: jsmoriss
1 Replies
Login or Register to Ask a Question