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";

--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

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
    It looks astonishing to me that there are so many ways to solve a single problem. I haven't understood your example at full now, but I will eventually. I have only learned if, elsif and else and how they work. :-D I won't be asking here what this and that is.The next things I will be searching:
    • Last
    • while
    And I will start studying the Learning perl book which a friend of mine has directed. This is my first ever program(except from hello world and similar programs from net)

      Heh. The motto of Perl is 'TMTOWTDI' - "There's More Than One Way To Do It". If you had asked to see different ways that people could implement that problem, you would have seen an amazing variety of ways to do it; in my experience, our fellow monks here are endlessly inventive. Here's a somewhat amusing one, off the top of my head:

      #!/usr/bin/perl -l use warnings; use strict; my @x; print "Please enter one number per line (empty line ends input):"; while (<STDIN>){ last if /^$/; push @x, $_; } print "The biggest number is ", (sort {$b<=>$a} @x)[0], "@x" =~ /^([-0-9]+)\s+(?:\1\s+)*$/ && "All numbers are the same";

      (Now don't let that distract you. "Learning Perl" is an excellent start for a Perl novice, so go study. :)


      -- 
      Education is not the filling of a pail, but the lighting of a fire.
       -- W. B. Yeats
      
      $biggest = ($a+$c+$b+$c+abs($a-$b)+abs($c-$a+$c-$b-abs($a-$b)))/4;
      --
      A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
      Online Fortune Cookie Search

      Don't use $a and $b :)

      use List::Util qw( max ); ($a, $b, $c) = (3, 12, 42); print +(sort { $b <=> $a } $a, $b, $c)[0]; print max ($a, $b, $c);

      These both work, but can you - as a perl beginner - still read it? Perl is so much fun due to TIMTOWTDI


      Enjoy, Have FUN! H.Merijn