Sponsored Content
Top Forums Shell Programming and Scripting Print is not in ordered after hash converted to array Post 302306050 by KevinADC on Friday 10th of April 2009 04:39:16 PM
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.
 

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
Hash::WithDefaults(3pm) 				User Contributed Perl Documentation				   Hash::WithDefaults(3pm)

NAME
Hash::WithDefaults - class for hashes with key-casing requirements supporting defaults version 0.05 SYNOPSIS
use Hash::WithDefaults; %main = ( ... ); tie %h1, 'Hash::WithDefaults', {...}; tied(%h1)->AddDefault(\%main); tie %h2, 'Hash::WithDefaults', [...]; tied(%h2)->AddDefault(\%main); # now if you use $h1{$key}, the value is looked up first # in %h1, then in %main. DESCRIPTION
This module implements hashes that support "defaults". That is you may specify several more hashes in which the data will be looked up in case it is not found in the current hash. Object creation tie %hash, 'Hash::WithDefault', [$case_option], [\%values]; tie %hash, 'Hash::WithDefault', [$case_option], [@values]; tie %hash, 'Hash::WithDefault', [$case_option], [%values]; The optional $case_option may be one of these values: Sensitive - the hash will be case sensitive Tolower - the hash will be case sensitive, all keys are made lowercase Toupper - the hash will be case sensitive, all keys are made uppercase Preserve - the hash will be case insensitive, the case is preserved Lower - the hash will be case insensitive, all keys are made lowercase Upper - the hash will be case insensitive, all keys are made uppercase If you pass a hash or array reference or an even list of keys and values to the tie() function, those keys and values will be COPIED to the resulting magical hash! After you tie() the hash, you use it just like any other hash. Functions AddDefault tied(%hash)->AddDefault(\%defaults); This instructs the object to include the %defaults in the search for values. After this the value will be looked up first in %hash itself and then in %defaults. You may keep modifying the %defaults and your changes WILL be visible through %hash! You may add as many defaults to one Hash::WithDefaults object as you like, they will be searched in the order you add them. If you delete a key from the tied hash, it's only deleted from the list of specific keys, the defaults are never modified through the tied hash. This means that you may get a default value for a key after you deletethe key from the tied hash! GetDefaults $defaults = tied(%hash)->GetDefaults(); push @$defaults, \%another_default; Returns a reference to the array that stores the defaults. You may delete or insert hash references into the array, but make sure you NEVER EVER insert anything else than a hash reference into the array! Config::IniHash example use Config::IniHash; $config = ReadIni $inifile, withdefaults => 1, case => 'preserve'; if (exists $config->{':default'}) { my $default = $config->{':default'}; foreach my $section (keys %$config) { next if $section =~ /^:/; tied(%{$config->{$section}})->AddDefault($default) } } And now all normal sections will get the default values from [:default] section ;-) AUTHOR
Jan Krynicky <Jenda@Krynicky.cz> http://Jenda.Krynicky.cz COPYRIGHT
Copyright (c) 2002-2009 Jan Krynicky <Jenda@Krynicky.cz>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-05-26 Hash::WithDefaults(3pm)
All times are GMT -4. The time now is 02:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy