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

Hello there!

I have a homework job to 1. Write a perl program that 1. ask a user to type 5 numbers. 2. display the numbers in the correct order on the screen.

Its not working out really swell though. So far i got this done

#! /usr/bin/perl print "\nType in 5 numbers.\nnumber1"; $a = <>; print "\nThe second number\n"; $b = <>; print "\nThe third number\n"; $c = <>; print "\nThe fourth number\n"; $d = <>; print "\nThe fifth number\n"; $e = <>; $bin =""; if ($a > $b {$bin = $b; $b = $a; $a = $bin; print "nThis is line 1 $a $b \n";} $a = <>; $b = <>; $c = <>; $d = <>; $e = <>; if ($b > $a) { print "\n This is line 2 $a $b \n";} if ($c > $d) {$bin = $d; $d = $c = $sbin; print "\n $c $d \n";}

I would very much appreciate it if you could point out my mistakes and give me some hints, or tips.

Replies are listed 'Best First'.
Re: Check this code please?
by Corion (Patriarch) on Aug 06, 2011 at 18:08 UTC

    The first, important, thing would be to describe how the program behaves, and how that is not what you want. Having a clear image of what goes wrong may already point you towards the solution. If it does not point you towards the solution, at least you can now easily tell others what you think is wrong with your program. Supplying sample data also is important to demonstrate how your program behaves, and how it should behave.

    The second, important, thing would be to use strict; and use warnings; at the top of your program. You have some variables that you only use once. This might be a typo. use warnings; will warn you about such situations. use strict; will force you to predeclare all your variables, so that mistyped names get prevented.

    Also, have you looked at the sort built-in?

    Consider the following input as an additional test case:

    10000 2000 300 40 5
Re: Check this code please?
by kcott (Archbishop) on Aug 06, 2011 at 21:46 UTC

    In addition to excellent points by Corion, here's a few more:

    • Avoid using $a and $b as they have special meaning when using sort (see code below). In general, I'd advise using more meaningful variable names than you have here.
    • The data returned by $x = <>; contains the newline entered from the keyboard, e.g. if I enter ABC then $x will contain the string "ABC\n". Not taking this into account is a common error. The chomp function removes these line terminators.
    • When gathering lists of input data, store them in an array.
    • When you see almost identical code repeated (like your input prompts here) there's almost always a better solution typically involving a loop.
    • Putting all that together, you could rewrite your input gathering code like this:
      my @keyed_data = map { print qq{Number $_: }; scalar <> } (1 .. 5);
      Note there's no checking of potentially unsafe, externally entered data nor does it allow for a variable range of lines.
    • In comparisons, Perl uses different operators for string and numerical data: see perlop - in particular, the sections Relational Operators and Equality Operators.
    • Many of Perl's functions operate on lists; some as input (e.g. print) and some as output (e.g. sort) (That's an oversimplification!). You can combine these functions such that the output of one becomes the input to another (see code below).
    • We can print the entire sorted list with just one line. Important: $a and $b are Perl's special variables not yours!
      print sort { $a <=> $b } @keyed_data;
      Note the output is completely unformatted except for a reliance on control characters embedded in the data - which is not good.
    • Take a look at sprintf for formatting strings and printf for printing them.
    • Your code could have been laid out a little better. Take a look at perlstyle. I think the code typo in your post:
      if ($a > $b {$bin = $b; $b = $a; $a = $bin; print "nThis is line 1 $a $b \n";}
      might have been more easily spotted if written like:
      if ($a > $b { $bin = $b; $b = $a; $a = $bin; print "nThis is line 1 $a $b \n"; }

    -- Ken

Re: Check this code please?
by jethro (Monsignor) on Aug 07, 2011 at 00:44 UTC

    Hint 1: Your script has 10 lines where the user has to type in numbers. Obviously too much when he should type in only 5

    Hint 2: Sorting the numbers is hell if you have them in single variables (i.e. $a,$b,$c...). I'm sure your teacher showed you how to use arrays. You need them here

    Hint 3: You could simply use the function sort() as kcott said. But if you want or need to sort without using that function, you will need loops, otherwise sorting the numbers is still hell

    Hint 4: There are a lot of methods to sort numbers (or anything else sortable). Do you know one? If not, think of how you would do it if you had them on paper and would have to sort them by hand, but could only see one number at a time (or compare and switch two numbers at a time).

    Generally most sorting algorithms do that in two steps: 1. Go through all numbers and do something so that the numbers are sorted a little better 2. Repeat step 1 until step 1 can't make the order any better. Note that both steps are loops, step 1 the inner loop, step 2 the outer loop.

Re: Check this code please?
by TomDLux (Vicar) on Aug 08, 2011 at 17:37 UTC

    I bet just before the assignment was set, recent classes dealt with loops and arrays. The intent is to have you use them in your solution.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Check this code please?
by Anonymous Monk on Aug 09, 2011 at 06:12 UTC
    $a = <>; $b = <>; $c = <>; $d = <>; $e = <>;

    This part is nonsense, this is where you want to print the results, not read in new data.

      PS: Unless I missed something, don't mind the monks too much, your question is clearly marked as homework and you ask for tips rather than solutions. You did the right thing here. They're just cranky cuz some monks are teachers (or something) and they get upset when someone asks for help with homework.

A reply falls below the community's threshold of quality. You may see it by logging in.