May 23, 2013

Decimal Sort in Perl


In the below mentioned, we have a hash with decimal values (for gpa key)
Lets assume we have a hash for students in the format mentioned below.
Let's discuss how to sort decimal values in Perl. We will sort both in ascending and descending ways.

decimal_sort.pl
use strict;
use warnings;
use Data::Dumper;

my %students = (
   'John'   => {
                    'Science'   => 'PASS',
                    'Maths'     => 'PASS',
                    'chemistry' => 'PASS',
                    'Physics'   => 'PASS',
                    'gpa'       => '4.01',
                  },

  'Diana'   => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.10',
               },

  'David'    => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.00',
                }
);

print "\n\n Before Sorting : ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Ascending Order : ";
for my $each_student (sort{ $students{$a}{gpa} <=> $students{$b}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Descending Order : ";
for my $each_student (sort{ $students{$b}{gpa} <=> $students{$a}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}
print "\n\n";
  


Output:

 Before Sorting : 
 Each Student : David   GPA : 4.00
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01

 After Sorting GPA in Ascending Order : 
 Each Student : David   GPA : 4.00
 Each Student : John    GPA : 4.01
 Each Student : Diana   GPA : 4.10

 After Sorting GPA in Descending Order : 
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01
 Each Student : David   GPA : 4.00
  


Please refer to other topics in Perl like :
Sort hash by value string in perl
Sort hash by value numerically in perl
Decimal Sort in Perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


No comments:

Post a Comment