Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Im trying to write a program that will get three numbers from STDIN im calling them $x,$y,$z I need to call a sub- rootine that will find the lowest number of the three I thought if I sorted them then retrived the first number it would be snazzy but i cant figure out how to get only one number to print sub

sub lowest {

@array = ("$x,$y,$z");

print join(' ', sort { $a <=> $b } @array, "\n"); }

I've been staring at this code trying different aproches for days now any sugetions would be much appeciated

Replies are listed 'Best First'.
Re: perl help sort
by archon (Monk) on Mar 09, 2001 at 23:52 UTC
    You are setting up your array incorrectly. You have an array of only one element. You want:
    push @array, $x, $y, $z; @array = sort{$a <=> $b} @array; print "$array[0]\n";
      your ausome thanks
Re: perl help sort
by danger (Priest) on Mar 10, 2001 at 01:26 UTC

    If it's just three numbers, here's a "snazzy" solution (for some definition of snazzy):

    my($x,$y,$z) = (11, 9, 12); print [[$x=>$y]->[$y<=$x]=>$z]->[$z<=[$x=>$y]->[$y<=$x]];

    Hey, I'm only kidding. Actually, [$x => $y]->[$x <= $y] was posted on clpm (see original here) as Perl's built-in max function. I just adapted it for 3 variables (reducing the efficiency even further, but maintaining reasonable symmetry :-)