I wrote the following program to test Perl's ability to evaluate ASCII values, and its if
/ elsif
statements.
I want to avoid using the language's sort function.
#test ASCII values using if and elsif
$s = '4';
$j = 'a';
$k = "AS";
#1st print the variable with the lowest ASCII value
if ($s <= $j && $s <= $k) {
say $s;
$s = 1000;
} elsif ($j <= $s && $j <= $k) {
say $j;
$j = 1000;
} elsif ($k <= $j && $k <= $s) {
say $k;
$k = 1000;
}
#print the variable with the 2nd lowest value
if ($s <= $j && $s <= $k) {
say $s;
$s = 1000;
} elsif ($j <= $s && $j <= $k) {
say $j;
$j = 1000;
} elsif ($k <= $j && $k <= $s) {
say $k;
$k = 1000;
}
#print the variable with the 3nd lowest value
if ($s <= $j && $s <= $k) {
say $s;
$s = 1000;
} elsif ($j <= $s && $j <= $k) {
say $j;
$j = 1000;
} elsif ($k <= $j && $k <= $s) {
say $k;
$k = 1000;
}
My output is as follows:
52
97
65
a
AS
4
but the correct outcome is:
52
97
65
4
AS
a
Thank you in advance. Tips on how to improve my question writing are welcome.