in reply to Find the biggest number from three numbers
In addition to the coding problems that others have pointed out, you also have a bit of a tool/problem mismatch: the job you're trying to do with your script is best handled by other, more suitable Perl control structures (it would be even easier with other functions and variable types, but we'll ignore that for now.) As a result, whatever solution you come up with is likely to be somewhat over-complicated and non-intuitive - i.e., a "complicated" if/then tree (which is where you got hung up.)
Here's an example that would do the job, although it may be outside the current scope of what you know. I've kept it as simple as possible.
#!/usr/bin/perl use warnings; use strict; print "Please enter one number per line (empty line ends input):\n"; my $different = 0; my $biggest = 0; # Loop and read user input while (1) { chomp(my $input = <STDIN>); if ($input eq ''){ last; } if ($biggest == 0){ $biggest = $input; } if ($input != $biggest){ $different = 1; } if ($input > $biggest){ $biggest = $input; } } if ($different == 0){ print "All numbers are the same\n"; } print "The biggest number is $biggest\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find the biggest number from three numbers
by shrsv (Novice) on Dec 23, 2010 at 12:49 UTC | |
by oko1 (Deacon) on Dec 26, 2010 at 02:56 UTC | |
by ysth (Canon) on Dec 26, 2010 at 07:42 UTC | |
by Tux (Canon) on Dec 26, 2010 at 09:23 UTC |