473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Hash sort weirdness

2
I am having trouble sorting secondary keys in a multi-level hash. I have a sample program that exhibits the problem...the following code is sorting New 6 Month before New 1 Month. What am I doing wrong?

Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.  
  4. my %data = (
  5.        "2008-03-24" => {
  6.                  "New 1 Month Subscription" => {
  7.                      id => 1,
  8.                      value => 10, 
  9.                  },
  10.                  "New 12 Month Subscription" => {
  11.                      id => 5,
  12.                      value => 12,
  13.                  },
  14.                  "New 14 Day Trial Subscription" => {
  15.                      id => 3,
  16.                      value => 15,
  17.                  },
  18.                  "New 6 Month Subscription" => {
  19.                      id => 6,
  20.                      value => 20,
  21.                  },
  22.        },
  23.        "2008-03-25" => {
  24.                  "New 1 Month Subscription" => {
  25.                      id => 1,
  26.                      value => 10, 
  27.                  },
  28.                  "New 12 Month Subscription" => {
  29.                      id => 5,
  30.                      value => 12,
  31.                  },
  32.                  "New 3 Month Subscription" => {
  33.                      id => 7,
  34.                      value => 8,
  35.                  },
  36.        },
  37. );
  38.  
  39. foreach my $this_date (sort keys %data) {
  40.   print "$this_date\n";
  41.   foreach my $name ( sort {lc($data{$a}) cmp lc($data{$b})} keys %{ $data{$this_date} } ) {
  42.      print "$name\t";
  43.      print "$data{$this_date}{$name}{id}\n";
  44.   }
  45. }
  46.  
Apr 1 '08 #1
4 1762
KevinADC
4,059 Expert 2GB
This line is wrong:

Expand|Select|Wrap|Line Numbers
  1. foreach my $name ( sort {lc($data{$a}) cmp lc($data{$b})} keys %{ 
should be:

Expand|Select|Wrap|Line Numbers
  1.   foreach my $name ( sort { $data{$this_date}{$a} cmp $data{$this_date}{$b} } keys %{ $data{$this_date} } ) {
but don't expect 6 to come after 12 when you sort using "cmp" because the sort is what is called asciibetical (sort of like alphabetical), in which case 12 comes before 6 like 'AC' comes before 'D'.

Note: there is no need to use lc() in your sort because all the characters are the same mix of upper and lower case characters. Use lc() or uc() when the strings being sorted are not all the same case.
Apr 1 '08 #2
cici
2
Ahh...I missed that. Now that I have that part corrected...I have tried to sort using both cmp and <=>...both are returning the same results which is:

2008-03-24
New 1 Month Subscription 1
New 12 Month Subscription 5
New 14 Day Trial Subscription 3
New 6 Month Subscription 6

I understand that is it using the ASCII value to sort right now, which is not what I want. How can I get it to sort the string part and then the numbers as numbers?

Thanks again...

This line is wrong:

Expand|Select|Wrap|Line Numbers
  1. foreach my $name ( sort {lc($data{$a}) cmp lc($data{$b})} keys %{ 
should be:

Expand|Select|Wrap|Line Numbers
  1.   foreach my $name ( sort { $data{$this_date}{$a} cmp $data{$this_date}{$b} } keys %{ $data{$this_date} } ) {
but don't expect 6 to come after 12 when you sort using "cmp" because the sort is what is called asciibetical (sort of like alphabetical), in which case 12 comes before 6 like 'AC' comes before 'D'.

Note: there is no need to use lc() in your sort because all the characters are the same mix of upper and lower case characters. Use lc() or uc() when the strings being sorted are not all the same case.
Apr 1 '08 #3
Ganon11
3,652 Expert 2GB
You can write your own short subroutine to sort them. In the subroutine, $a will be the first element and $b will be the second element. You take $a and $b and parse them for the number (if you know that's the only place they will differ), and then return the comparison of those numbers.

The easiest way to parse the strings will be a regex - just match for the proper pattern and capture each number in $1.
Apr 1 '08 #4
KevinADC
4,059 Expert 2GB
Ahh...I missed that. Now that I have that part corrected...I have tried to sort using both cmp and <=>...both are returning the same results which is:

2008-03-24
New 1 Month Subscription 1
New 12 Month Subscription 5
New 14 Day Trial Subscription 3
New 6 Month Subscription 6

I understand that is it using the ASCII value to sort right now, which is not what I want. How can I get it to sort the string part and then the numbers as numbers?

Thanks again...

This is a job for the Schwartzian Transform
Apr 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tommo | last post by:
Hello All, I have a slight problem that goes like this. I have created a Perl hash where the keys are made up of numeric values, I was then sorting the 'keys' for this hash on their value (a<=>b...
3
by: Casey | last post by:
Hi, I haven't being using perl for too long. Can someone explain the correct way to get the sort function to recognize an anonymous function declared as a hash value? Look at my sample code for...
34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
6
by: thecodemachine | last post by:
Hi, I'm looking for a fast and simple one to one hash function, suitable for longer strings (up to 2048 in length). I'd like keys to be relatively short, I doubt I'd be creating more than 256...
2
by: Bobby Chamness | last post by:
I have a perl script that I wrote the loops through a list of servers in a file and I want to create a hash with the server name in it. I want each server to have its own hash. for example...
12
by: DaveMM | last post by:
I am a novice perl programmer, and I am trying to sort a hash by a value in the hash and I am having problems. I've done this before, but this time something is going wrong and I'm at a loss. I'm...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
5
by: nirmal1349 | last post by:
Hi, I have a hash in hash file in perl which looks some thing like this: $FIELDS = { 'abc' => { 'Description' => { 'Purpose' => 'some data is present',...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.