Hi,
Lets say I have the following hash structure in perl: -
my %products (
-
1 => {
-
name => "Floor Wax",
-
wholesale => "50.00",
-
retail => "abc1.doc",
-
category => "abc",
-
},
-
2 => {
-
name => "Paper Towel",
-
wholesale => "20.00",
-
retail => "abc2.doc",
-
category => "abc",
-
},
-
3 => {
-
name => "Hand Soap",
-
wholesale => "30.00",
-
retail => "xyz1.doc",
-
category => "xyz",
-
},
-
);
-
I want the o/p as :
-
-
abc
-
abc1.doc
-
abc2.doc
-
xyz
-
xyz1.doc
-
pls tel me how will I perform sorting on this hash.
Reply asap...
4 3195
A hash has no order. You cannot sort the hash directly. What you can do is sort the keys of the hash (by getting the keys in a list using the keys function), and then access the hash with those keys in sorted order.
You will need to write a short subroutine to say how you are sorting: i.e. you and not comparing the keys, but the field of the hash referred to by the key.
Have you tried to sort the data?
reply ASAP....
hi,
I tried sortin the data using smethng like this: -
for my $key1 (reverse sort { $mydata{$b}{'Category'} cmp $mydata{$a}{'Category'} } keys %mydata) {....
-
-
}
-
here i can get the hash sorted using Category but then I need to go deep and further sort it by the retail value...
i tried using diffrnt loops but cld not arive at the answer..
Reply asap
one possible way: - my %products = (
-
1 => {
-
name => "Floor Wax",
-
wholesale => "50.00",
-
retail => "abc1.doc",
-
category => "abc",
-
},
-
2 => {
-
name => "Paper Towel",
-
wholesale => "20.00",
-
retail => "abc2.doc",
-
category => "abc",
-
},
-
3 => {
-
name => "Hand Soap",
-
wholesale => "30.00",
-
retail => "xyz1.doc",
-
category => "xyz",
-
},
-
4 => {
-
name => "Hand Soap",
-
wholesale => "30.00",
-
retail => "xyz2.doc",
-
category => "xyz",
-
},
-
);
-
-
my @sorted = map{"$_->[2] $_->[3] $_->[0] $_->[1]"}
-
sort{$a->[3] cmp $b->[3] || $a->[2] cmp $b->[2]}
-
map{ [$products{$_}{'name'},$products{$_}{'wholsale'},$products{$_}{'retail'},$products{$_}{'category'}] } keys %products;
-
-
print "$_\n" for @sorted;
Sign in to post your reply or Sign up for a free account.
Similar topics
by: pillbug |
last post by:
Hi,
I'm trying to tie a hash that is returned from a DBI
method called fetchrow_hashref. All I want to do is
override the FETCH method to validate my key names.
But fetchrow_hashref returns...
|
by: Christopher |
last post by:
I have a hash that is several levels deep.
ie:
'vars' => {
'$filers' => '10.10.10.10/32',
'$networksa' => '10.10.10.10/32',
'$networksb' => '10.50.0.0/16',
'$wintel_boxes' =>...
|
by: Xah Lee |
last post by:
Sort a List
Xah Lee, 200510
In this page, we show how to sort a list in Python & Perl and also
discuss some math of sort.
To sort a list in Python, use the “sort” method. For example:
...
|
by: Dan Stromberg |
last post by:
Hi folks.
Python appears to have a good sort method, but when sorting array elements
that are very large, and hence have very expensive compares, is there some
sort of already-available sort...
|
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...
|
by: KevinADC |
last post by:
Introduction
This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
|
by: KevinADC |
last post by:
Introduction
In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
|
by: KevinADC |
last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
|
by: dillipkumar |
last post by:
Hi,
I have a Hash:
%hash= 'student' => ,
I want to sort the hash according to sorting of 'roll_no' ..
After sorting It should be displayed
%hash= 'student' => ,
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shllpp 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |