Hi,
I have a Hash: - %hash= 'student' => [
-
{
-
'roll_no' => 10,
-
'sub' => 'eng'
-
marks => 32,
-
},
-
{
-
'roll_no' => 11,
-
'sub' => 'math'
-
marks => 69,
-
},
-
{
-
'roll_no' => 10
-
'sub' => 'science'
-
marks => 69,
-
},
-
-
{
-
'roll_no' => 25
-
'sub' => 'geo'
-
marks => 59
-
},
-
-
],
I want to sort the hash according to sorting of 'roll_no' ..
After sorting It should be displayed - %hash= 'student' => [
-
{
-
'roll_no' => 10,
-
'sub' => 'eng'
-
marks => 32,
-
},
-
{
-
'roll_no' => 10
-
'sub' => 'science'
-
marks => 69,
-
},
-
-
{
-
'roll_no' => 11,
-
'sub' => 'math'
-
marks => 69,
-
},
-
-
{
-
'roll_no' => 25
-
'sub' => 'geo'
-
marks => 59
-
},
-
-
],
Can anybody help me...
12 2211
Is this school/class/course work?
A hash has no order. If you want to have data sorted, use an array instead.
A hash has no order. If you want to have data sorted, use an array instead.
It is possible but I would also recommend using an array. See Tie::SortHash and Tie::IxHash.
Hi,
Actually this is a task..
The Original Hash is like this: - %hash = ( 'difference' => [
-
{
-
'line_number' => '21',
-
'filename' => 'BRANCH27',
-
'source_line' => {},
-
'target_line' => '001S TR'
-
},
-
{
-
'line_number' => '1',
-
'filename' => 'BRANCH',
-
'source_line' => {},
-
'target_line' => '023X '
-
},
-
{
-
'line_number' => '140',
-
'filename' => 'BRANCH',
-
'source_line' => {},
-
'target_line' => '139FIFTH'
-
},
-
{
-
'line_number' => '1',
-
'filename' => 'ACCOUNT27',
-
'source_line' => {},
-
'target_line' => '0223X '
-
},
-
],
-
-
);
i want to sort this hash..
A hash has no order. If you want to have data sorted, use an array instead.
Hi The Original hash is like this: - %hash = ( 'difference' => [
-
{
-
'line_number' => '21',
-
'filename' => 'BRANCH27',
-
'source_line' => {},
-
'target_line' => '001S TR'
-
},
-
{
-
'line_number' => '1',
-
'filename' => 'BRANCH',
-
'source_line' => {},
-
'target_line' => '023X '
-
},
-
{
-
'line_number' => '140',
-
'filename' => 'BRANCH',
-
'source_line' => {},
-
'target_line' => '139FIFTH'
-
},
-
{
-
'line_number' => '1',
-
'filename' => 'ACCOUNT27',
-
'source_line' => {},
-
'target_line' => '0223X '
-
},
-
],
-
-
);
Hi The Original hash is like this:
%hash = ( 'difference' => [
{
'line_number' => '21',
'filename' => 'BRANCH27',
'source_line' => {},
'target_line' => '001S TR'
},
{
'line_number' => '1',
'filename' => 'BRANCH',
'source_line' => {},
'target_line' => '023X '
},
{
'line_number' => '140',
'filename' => 'BRANCH',
'source_line' => {},
'target_line' => '139FIFTH'
},
{
'line_number' => '1',
'filename' => 'ACCOUNT27',
'source_line' => {},
'target_line' => '0223X '
},
],
);
The structure is hash of array of hashes. To display it sorted according to 'line_number' foreach top-level key('difference'), you would be actually sorting the array and not the hash (according to the intended output you posted in your initial description).
You can use: -
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(keys %{$a});
-
print "\n";
-
}
-
}
-
The structure is hash of array of hashes. To display it sorted according to 'line_number' foreach top-level key('difference'), you would be actually sorting the array and not the hash (according to the intended output you posted in your initial description).
You can use: -
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(keys %{$a});
-
print "\n";
-
}
-
}
-
Hi Nitin,
Now it is working but the 'source_line' returns the hash like:
source_line : HASH(0x81ffdac)
Hi Nitin,
Now it is working but the 'source_line' returns the hash like:
source_line : HASH(0x81ffdac)
That is because you have an empty hash as value for 'source_line' key.
That is because you have an empty hash as value for 'source_line' key.
Actually after executing it is displaying this format --> - {
-
filename' => 'BRANCH27',
-
line_number' => '21',
-
'source_line' => {},
-
'target_line' => '001S TR'
-
}
-
-
But the Actually Hash is in this format:
-
{
-
'line_number' => '21',
-
'filename' => 'BRANCH27',
-
'source_line' => {},
-
'target_line' => '001S TR'
-
},
Actually after executing it is displaying this format -->
{
'line_number' => '21',
'filename' => 'BRANCH27',
'source_line' => {},
'target_line' => '001S TR'
},
But the Actually Hash is in this format:
{
filename' => 'BRANCH27',
line_number' => '21',
'source_line' => {},
'target_line' => '001S TR'
}
That's typically the behaviour of hash, the order won't be maintained. If you want to retain to retain the order, you can pass the order of keys into an array and use it while displaying. -
@keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
-
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(@keys); ## use @keys here
-
print "\n";
-
}
-
}
-
You would not have had this question if you had gone through Tie::SortHash that KevinADC mentioned in his reply.
That's typically the behaviour of hash, the order won't be maintained. If you want to retain to retain the order, you can pass the order of keys into an array and use it while displaying. -
@keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
-
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(@keys); ## use @keys here
-
print "\n";
-
}
-
}
-
You would not have had this question if you had gone through Tie::SortHash that KevinADC mentioned in his reply.
Thank Nitin for ur help...
dillipkumar,
Four out of the the five posts that you have made in this tread you have neglected to use the code tags. Please use the code tags when posting code and data samples on this site.
[CODE]...your code goes here...[/CODE]
Thank You!
--Kevin
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dr. zoidberg |
last post by:
Hello,
I have mysql table:
+----+---------+
| 1 | value1 |
+----+---------+
| 2 | value2 |
+----+---------+
| 3 | value3 |
|
by: Me |
last post by:
I have a hash (of a hashes) that has data like this
table1->table_name
->table_size
table2->table_name
->table_size
|
by: Tommo |
last post by:
Hello All,
whilst I know how to sort an array I am having problems with
my situation, what I would ideally like is something like a 'sort
keys' action with Perl hashes.
I am reading in data and...
|
by: VK |
last post by:
Or why I just did myArray = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they...
|
by: guitarromantic |
last post by:
Hey,
I have a 'staff' table with a column 'status', corresponding to a
'statuses' table with the appropriate titles (eg 1 | Editor in Chief).
I want to display on the page the headings (the...
|
by: john sutor |
last post by:
Does anyone know how to sort a hash table?
|
by: Fred Morrison |
last post by:
Do you know of a way to load a hash table with random key/value pairs (e.g.,
2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted"
(key sequence) order...
|
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: 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: 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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |