[WpProQuiz 2]
[WpProQuiz_toplist 2]
Perl Quiz on Data Types
Learning through sharing
[WpProQuiz 2]
[WpProQuiz_toplist 2]
Perl is such a nice language that even a beginner can write the fully working code by using simpler things available in Perl. More you know about Perl more crazy you will be about it. Its very famous for one liner stuffs and even it has many functions which can minimize our code, run time and our work too!
Map function in Perl is a nice example for it. You can replace you whole foreach code with map in a single line.
Note: Block does not need comma but expression needs coma explicitly see the examples using { block } and (expression). In simple ,it is
For fully working code on map function please visit this link For more details on map click here
Sorting an array and hash elements in PerlStandard string comparison means based on ASCII value of those characters. Like @arr = qw (Call all). In this case it will be sorted as Call all which was not expected. So to make it work properly we use case-insensitive sort.
If SUBNAME is specified, it gives the name of a subroutine that returns an integer less than, equal to, or greater than 0 , depending on how the elements of the list are to be ordered. (The <=> and cmp operators are extremely useful in such routines.)
Note: The values to be compared are always passed by reference and should not be modified . $a and $b are global variable and should not be declared as lexical variables.
sort() returns aliases into the original list like grep, map etc which should be usually avoided for better readability.
As sorting always does string sorting, so to do numeric sorting we need to use a special syntax which a sort {$a ó $b} LIST. We will see these conditions using Perl codes.
How reverse sorting works
Systax to use reverse sort is reverse LIST. It works on sorted LIST usually. But in scalar context, it concatenates the elements of LIST and returns a string value with all characters in the opposite order.
In scalar context if argument is not passed it will reverse the value of $_
Ex:
[perl]$_ = “dlrow ,olleH”;
print scalar reverse; #in this case print reverse would not works because it expects a LIST
[/perl]
Try this out:
[perl]
my @arr1 = qw(1 two 3 0 4 Two 5 six 7 8 9 ten);
my @arr2 = sort {$a cmp $b} @arr1;
print “\n@arr2\n”;
[/perl]
Let go through the codes for different scenarios:
Example 1: Sorting an array of strings (case-sensitive and case-insensitive examples)
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @strarr = qw(two Two six Six alien Coders Alien coderS);
my @sorted = sort {$a cmp $b} @strarr; # same result as of sort @strarr
my @sortedcase = sort { uc $a cmp uc $b } @strarr; #case-insensitivie
print “\n@sorted\n@sortedcase\n”;
[/perl]
Output:
Alien Coders Six Two alien coderS six two
alien Alien Coders coderS six Six two Two
Note: try to always use case insensitive for better string sorting results.
Example 2: Sorting an array of numbers
The Perl sort function sorts by strings instead of by numbers. If you do it in general way it would fetch unexpected result.
[perl] #!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
my @sorted_numbers = sort @numbers;
print “@sorted_numbers\n”;
[/perl]
The output you would see would be:
001 01 1 109 22 23 3 65 7 9
To sort numerically, declare your own sort block and use the binary equality operator i.e. flying saucer operator <=>:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
my @sorted_numbers = sort {$a <=> $b} @numbers;
print “@sorted_numbers\n”;
[/perl]
The output would now be:
1 01 001 3 7 9 22 23 65 109
Note that $a and $b do not need to be declared, even with use strict on, because they are special sorting variables.
Example 3: Sorting array backwards (for string and numbers)
To sort backwards you need to declare your own sort block, and simply put $b before $a. or use reverse keyword after simple sort.
For example, the standard sort is as follows:
[perl] #!/usr/bin/perl
use strict;
use warnings;
my @strings = qw(Jassi Alien Coders);
my @sorted_strings = sort @strings;
print “@sorted_strings\n”;
[/perl]
The output would be:
Alien Coders Jassi
To do the same, but in reverse order:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @strings = qw(Jassi Alien Coders);
my @sorted_strings = sort {$b cmp $a} @strings; # or reverse sort @strings
print “@sorted_strings\n”;
[/perl]
The output is:
Jassi Coders Alien
And for numbers:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @numbers = (23, 1, 22, 7, 109, 9, 65, 3);
my @sorted_numbers = sort {$b <=> $a} @numbers; # or reverse sort {$aó $b} @numbers
print “@sorted_numbers\n”;
[/perl]
The output is:
109 65 23 22 9 7 3 1
This was all about sorting array elements alphabetically or numerically. Now we will see how sorting works on hash elements.
Example 4: Sorting hashes by keys
You can use sort to order hashes. For example, if you had a hash as follows:
Suppose we want to display the members for each community sorted alphabetically or say by keys, then this code will do so:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my %members = (
C => 1,
Java => 7,
Perl => 12,
Linux => 3,
Hacking => 8,
);
foreach my $language (sort keys %members) {
print $language . “: ” . $members{$language} . “\n”;
}
[/perl]
Output:
C: 1
Hacking: 8
Java: 7
Linux: 3
Perl: 12
If you want to sort the same hash by the values (i.e. the users beside each programming language), you could do the following:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my %members = (
C => 1,
Java => 7,
Perl => 12,
Linux => 3,
Hacking => 8,
);
# Using <=> instead of cmp because of the numbers
foreach my $language (sort {$members{$a} <=> $members{$b}} keys %members){
print $language . “: ” . $members{$language} . “\n”;
}
[/perl]
Output:
C: 1
Linux: 3
Java: 7
Hacking: 8
Perl: 12
Example: 5 Sorting complex data structures
We can also use sort function to sort complex data structures. For example, suppose we have an array of hashes (anonymous hashes) like:
[perl]
my @aliens = (
{ name => ‘Jassi’, age => 28},
{ name => ‘Somnath’, age => 27},
{ name => ‘Ritesh’, age => 24},
{ name => ‘Santosh’, age => 29},
{ name => ‘Ranjan’, age => 26},
{ name => ‘Kaushik’, age => 25},
);
[/perl]
And we wish to display the data about the people by name, in alphabetical order, we could do the following:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @aliens = (
{ name => ‘Jassi’, age => 28},
{ name => ‘Somnath’, age => 27},
{ name => ‘Ritesh’, age => 24},
{ name => ‘Santosh’, age => 29},
{ name => ‘Ranjan’, age => 26},
{ name => ‘Kaushik’, age => 25},
);
foreach my $person (sort {$a->{name} cmp $b->{name}} @aliens) {
print $person->{name} . ” is ” . $person->{age} . “\n”;
}
[/perl]
The output is:
Jassi is 28
Kaushik is 25
Ranjan is 26
Ritesh is 24
Santosh is 29
Somnath is 27
Sorting the same hash by age and using a subroutine (inline function)
Rather than writing the code inline, you can also pass in a subroutine name. The subroutine needs to return an integer less than, equal to, or greater than 0. Do not modify the $a and $b variables as they are passed in by reference, and modifying them will probably confuse your sorting.
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @aliens = (
{ name => ‘Jassi’, age => 28},
{ name => ‘Somnath’, age => 27},
{ name => ‘Ritesh’, age => 24},
{ name => ‘Santosh’, age => 29},
{ name => ‘Ranjan’, age => 26},
{ name => ‘Kaushik’, age => 25},
);
foreach my $person (sort agecomp @aliens) {
# it just replaced {$a->{age} <=> $b->{age}} by agecomp inline function
print $person->{name} . ” is ” . $person->{age} . ” years old\n”;
}
sub agecomp {
$a->{age} <=> $b->{age};
}
[/perl]
The output would be:
Ritesh is 24 years old
Kaushik is 25 years old
Ranjan is 26 years old
Somnath is 27 years old
Jassi is 28 years old
Santosh is 29 years old
To find out more on sort function, run the command on Linux box:
perldoc -f sort
It happens many times that you declare a variable name something and calling that variable name by something else. In case of hash in perl, calling that variable or assigning that variable will not give error. It will silently create that key value pair. This is called autovivification.
For ex:
my $name = "Jassi";
$self->{name} = "Sanjeev aka Jassi" # it will work as it is
$self->{Name} = "Jaiswal"; # It will silently create another key value pair.
# Now if you try to access $self->{name}, it will not give you
#Jaiswal. You will get "Sanjeev aka Jassi".
At above example you can see that our motto was to change the value of key “name” but by mistake we wrote “Name”, then also it got updated without any error. So being a human being, we do such typo mistakes and It will be very tough for us to find such bugs.
Before writing a program, lets keep in mind that what variables we are going to use and strict the program to use those variable name only. If I use any other variable than provided one, it should throw error. For this purpose we use “field” pragma.
#!c:/strawberry/perl/bin/perl.exe
{
package Student;
# You may like to give a list of attributes that are only allowed, and have Perl should exit with an error,
#if someone uses an unknown attribute.
# You can do this using the fields pragma i.e use fields fieldname1, fieldname2 etc"
# Isn't it nice feature to secure our code.
use fields 'name',
'registration',
'grades';
sub new {
my($class, $name) = @_;
$self = fields::new($class); # returns an empty "strict" object
$self->{name} = $name; # attributes get accessed as usual
return $self; # $self is already blessed
}
}
my $classname = Student->new('Jassi');
$classname->{name} = 'Jassi D\'Costa'; # works
$classname->{Name} = 'foo'; # blows up. If I will not use field pragma, It will silently create this key value pair
# for $classname object.
# End of the program
Here is the screen-shot of the error that you will get.
Subscribe now to keep reading and get access to the full archive.