perl hash sort


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl hash sort
# 1  
Old 01-05-2011
perl hash sort [ SOLVED ]

I have a hash as below

Code:
'C1' => { 'x' => 41.9 , 'y' => 5.79999999999995}
'c2 288' => { 'x' => 428.05 , 'y' => 5.79999999999995}
'turn' => { 'x' => 493.25 , 'y' => 209.85}
'0001' => { 'x' => 530.1 , 'y' => 195.7}
'000001' => { 'x' => 235.25 , 'y' => 728.15}
'XYZ' => { 'x' => 56.65 , 'y' => 716.7}
'AS' => { 'x' => 78.7 , 'y' => 716.7}
'HOUSE' => { 'x' => 56.65 , 'y' => 708.3}
'dsf' => { 'x' => 89.75 , 'y' => 708.3}
'YIPPE' => { 'x' => 106.3 , 'y' => 708.3}
'DAY' => { 'x' => 150.45 , 'y' => 708.3}
'12' => { 'x' => 56.65 , 'y' => 699.9}
'INGG' => { 'x' => 95.25 , 'y' => 699.9}
'CORNER' => { 'x' => 122.85 , 'y' => 699.9}
'Light 049319' => { 'x' => 56.65 , 'y' => 691.6}
'(023117)' => { 'x' => 164.8 , 'y' => 678.35}
'01201' => { 'x' => 57.5 , 'y' => 5.79999999999995}

I need it to sort with x first and then y ... and then print as per x and y values key .
Please suggest if it is possible to sort an hash twice as required and then print ?

Last edited by chakrapani; 01-05-2011 at 09:17 AM..
# 2  
Old 01-05-2011
Here is an article on sorting a hash of hashes
# 3  
Old 01-05-2011
Thanks @citaylor it will only sort with one value at a time. I want to sort my data with X first and then same data be sorted by Y.
# 4  
Old 01-05-2011
Code:
perl -e'
      
 %h = (  
    C1 => { x => 41.9 , y => 5.79999999999995},
    "c2 288" => { x => 428.05 , y => 5.79999999999995},
    turn => { x => 493.25 , y => 209.85},
    "0001" => { x => 530.1 , y => 195.7},
    "000001" => { x => 235.25 , y => 728.15},
    XYZ => { x => 56.65 , y => 716.7},
    AS => { x => 78.7 , y => 716.7},
    HOUSE => { x => 56.65 , y => 708.3},
    dsf => { x => 89.75 , y => 708.3},
    YIPPE => { x => 106.3 , y => 708.3},
    DAY => { x => 150.45 , y => 708.3},
    12 => { x => 56.65 , y => 699.9},
    INGG => { x => 95.25 , y => 699.9},
    CORNER => { x => 122.85 , y => 699.9},
    "Light 049319" => { x => 56.65 , y => 691.6},
    "(023117)" => { x => 164.8 , y => 678.35},
    "01201" => { x => 57.5 , y => 5.79999999999995},
    );
    
    print map {
        $h{$_}{x}, " ", $h{$_}{y}, $/    
        }
        sort {
            $h{$a}{x} <=> $h{$b}{x} ||
            $h{$a}{y} <=> $h{$b}{y}
                } keys %h;
    '

With your data it outputs:

Code:
41.9 5.79999999999995
56.65 691.6
56.65 699.9
56.65 708.3
56.65 716.7
57.5 5.79999999999995
78.7 716.7
89.75 708.3
95.25 699.9
106.3 708.3
122.85 699.9
150.45 708.3
164.8 678.35
235.25 728.15
428.05 5.79999999999995
493.25 209.85
530.1 195.7


Last edited by radoulov; 01-05-2011 at 09:16 AM.. Reason: Special key strings quoted.
# 5  
Old 01-05-2011
Wow that is good ... Thanks Smilie

Could you let me know how I could change it to print data (first part) not x and y value ...
# 6  
Old 01-05-2011
You mean the hash keys?

Code:
perl -le'
      
 %h = (  
    C1 => { x => 41.9 , y => 5.79999999999995},
    "c2 288" => { x => 428.05 , y => 5.79999999999995},
    turn => { x => 493.25 , y => 209.85},
    "0001" => { x => 530.1 , y => 195.7},
    "000001" => { x => 235.25 , y => 728.15},
    XYZ => { x => 56.65 , y => 716.7},
    AS => { x => 78.7 , y => 716.7},
    HOUSE => { x => 56.65 , y => 708.3},
    dsf => { x => 89.75 , y => 708.3},
    YIPPE => { x => 106.3 , y => 708.3},
    DAY => { x => 150.45 , y => 708.3},
    12 => { x => 56.65 , y => 699.9},
    INGG => { x => 95.25 , y => 699.9},
    CORNER => { x => 122.85 , y => 699.9},
    "Light 049319" => { x => 56.65 , y => 691.6},
    "(023117)" => { x => 164.8 , y => 678.35},
    "01201" => { x => 57.5 , y => 5.79999999999995},
    );
    
    print join $/,
        sort {
            $h{$a}{x} <=> $h{$b}{x} ||
            $h{$a}{y} <=> $h{$b}{y}
                } keys %h;
    '

Ouput:

Code:
C1
Light 049319
12
HOUSE
XYZ
01201
AS
dsf
INGG
YIPPE
CORNER
DAY
(023117)
000001
c2 288
turn
0001

# 7  
Old 01-05-2011
yes That it ... Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Sort a hash based on the string length of the values

Hi, I want to be able to sort/print a hash based on the string length of the values. For example %hash = ( key1 => 'jeri', key2 => 'corona', key3 => 'una, ); I want to be able to print in the following order (smallest to largest) una,jeri,corona OR... (1 Reply)
Discussion started by: jdilts
1 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

perl hash

Hi i am reading one file and creating Hash from the contents of it my issue is there are 3 different files in 3 different locations having same structure so for parsing these files i have one subroutine which returns hash after reading all the 3 files i need to create consolidated hash from three... (2 Replies)
Discussion started by: zedex
2 Replies

8. Shell Programming and Scripting

perl hash

This is my data 1 0 1 0 1 1 1 2 1 6 1 7 Assume that first field is key and 2nd field is value I want to create a hash in perl, on this data. My hash should having uniq key and all values by , separated. 1,0,0,1,2,6,7 1 will be my key and rest of are should be values. (3 Replies)
Discussion started by: pritish.sas
3 Replies

9. Shell Programming and Scripting

perl hash

i have an hash table in which each value is an array. How can i print for each key the array values??? something like this: thanks (2 Replies)
Discussion started by: littleboyblu
2 Replies

10. Shell Programming and Scripting

perl help on hash

I have line which is read from xml doc. I want to put this line into hash(perl variable). find line below and how i want to put this in hash <font size="10" type="int" name="ABC" > hash key should be size, type and name with corresponding value I doing as below:- $line =~ s/\s*.*?\s//;... (3 Replies)
Discussion started by: aju_kup
3 Replies
Login or Register to Ask a Question