Sponsored Content
Top Forums Shell Programming and Scripting Print is not in ordered after hash converted to array Post 302305046 by teiji on Tuesday 7th of April 2009 10:14:02 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Tie::Hash::NamedCapture(3perl)				 Perl Programmers Reference Guide			    Tie::Hash::NamedCapture(3perl)

NAME
Tie::Hash::NamedCapture - Named regexp capture buffers SYNOPSIS
tie my %hash, "Tie::Hash::NamedCapture"; # %hash now behaves like %+ tie my %hash, "Tie::Hash::NamedCapture", all => 1; # %hash now access buffers from regexp in $qr like %- DESCRIPTION
This module is used to implement the special hashes "%+" and "%-", but it can be used to tie other variables as you choose. When the "all" parameter is provided, then the tied hash elements will be array refs listing the contents of each capture buffer whose name is the same as the associated hash key. If none of these buffers were involved in the match, the contents of that array ref will be as many "undef" values as there are capture buffers with that name. In other words, the tied hash will behave as "%-". When the "all" parameter is omitted or false, then the tied hash elements will be the contents of the leftmost defined buffer with the name of the associated hash key. In other words, the tied hash will behave as "%+". The keys of "%-"-like hashes correspond to all buffer names found in the regular expression; the keys of "%+"-like hashes list only the names of buffers that have captured (and that are thus associated to defined values). SEE ALSO
perlreapi, re, "Pragmatic Modules" in perlmodlib, "%+" in perlvar, "%-" in perlvar. perl v5.14.2 2011-09-19 Tie::Hash::NamedCapture(3perl)
All times are GMT -4. The time now is 04:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy