mardi 18 octobre 2016

perl exercise - comparing ascii values

I am learning Perl, I wrote the following program to test Perl's ability to evaluate of ASCII values, and its if/elseif statements. I want to avoid using the languages sort function.

#test values
$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 advanced. Tips on how to improve my question writing are welcome.

Aucun commentaire:

Enregistrer un commentaire