Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
sort hash?
herald10
Hi,
I have a set of properties that I need to display by state. When displaying first it needs to be sorted by the state and then for each state all the properties need to be sorted by the property names. I am thinking of using arrays. But I remember some one mentioning in the forum that hashes are the best in perl. Can any one provide me some guidance as to which way to go with this.
Thanks a lot
-H
Find more posts tagged with
Comments
Adam Stoller
Hashes, or associative arrays, are a strong part of Perl - but you don't seemed to have provided sufficient information to respond to here.
What does your data look like?
How do you want it to appear in the first place ?
How do you want it to appear in the second place ?
If you can provide a sample (say 5-10 pieces of data and examples of what you want to see) - it will be a lot easier to show you how it would work.
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
herald10
Here we go. I want it to appear some thing like:
Arizona
property1
property2
property3
California
property1
property2
property3
Colorado
property1
property2
property3
Florida
property1
property2
property3
Maryland
property1
property2
property3
As you can see sorting is first done by states and then by properties with in each state.
Thanks a lot
-H
ReportIssue.jpg
Adam Stoller
# This defines a hash in which the key's are states, and the values
# are a reference to an array of properties
# (I assume the real properties will have more distinguished labels)
#
# I have purposfully mixed up the sorting of both the states
# and the properties to validate that the end result *is* sorted
my %hash =
(
California => [qw(property1 property3 property2)],
Arizona => [qw(property2 property1 property3)],
Maryland => [qw(property3 property1 property2)],
Colorado => [qw(property1 property3 property2)],
Florida => [qw(property2 property1 property3)],
);
# Sort through the key values first
foreach(sort(keys(%hash))){
# for each entry, print out the sorted elements of the
# value associated with the key
print "$_\n\t", join("\n\t", sort(@{$hash{$_}})), "\n";
}
Does that help?
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
herald10
Ghoti,
Ran your code. It perfectly fits the bill. Thanks a lot. I have one more question. Each property has a URL associated with it. So I am thinking of maintaining a seperate hash of URLs for the properties. Is it ok or should I include URLs in %hash itself. But then that would probably complicate the sorting part, I guess. Please correct me if I am wrong.
Thanks
-H
Adam Stoller
This is a bit more complex, but still relatively simple in the overall scheme of things:
# This defines a hash in which the key's are states, and the values
# are a reference to simple hashes of properties and their associated urls
# (I assume the real properties will have more distinguished labels)
#
# I have purposfully mixed up the sorting of both the states
# and the properties to validate that the end result *is* sorted
my %hash =
(
California => {property1 => "
http://foo
",
property3 => "
http://bar
",
property2 => "
http://baz
"},
Arizona => {property2 => "
http://eenie
",
property1 => "
http://meenie
",
property3 => "
http://miney
"},
Maryland => {property3 => "
http://harpo
",
property1 => "
http://chico
",
property2 => "
http://groucho
"},
Colorado => {property1 => "
http://this
",
property3 => "
http://that
",
property2 => "
http://theother
"},
Florida => {property2 => "
http://sleepy
",
property1 => "
http://bashful
",
property3 => "
http://doc
"},
);
# Sort through the key values first
foreach my $k (sort(keys(%hash))){
# for each entry, print out the sorted elements of the
# value associated with the key and it's url
print "$k\n";
foreach(sort(keys(%{$hash{$k}}))){
print "\t$_ (${$hash{$k}}{$_})\n";
}
}
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com