Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to make perl sort these values in order?

by rse2 (Initiate)
on Aug 07, 2011 at 00:24 UTC ( [id://919008]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I wrote a program where a user types in five numbers, and then it displays the numbers in the order of which they were typed. But i need to make my program Print the numbers that the user enters from least to greatest. Any idea how i can do this? So far i got.

#! /usr/bin/perl print "Type in a number."; $n1 = <>; print "Type in another number."; $n2 = <>; print "Type in another number."; $n3 = <>; print "Type in another number."; $n4 = <>; print "Type in another number."; $n5 = <>; print "$n1 \n"; print "$n2 \n"; print "$n3 \n"; print "$n4 \n"; print "$n5 \n";

Thanks a million.

Replies are listed 'Best First'.
Re: How to make perl sort these values in order?
by davido (Cardinal) on Aug 07, 2011 at 01:25 UTC
Re: How to make perl sort these values in order?
by james2vegas (Chaplain) on Aug 07, 2011 at 00:41 UTC
    First off you should chomp the values you get from readline (<>), then sort them, the default sort is not numeric so something like:

    #!/usr/bin/perl use strict; use warnings; print "Type in a number."; chomp(my $n1 = <>); print "Type in another number."; chomp(my $n2 = <>); print "Type in another number."; chomp(my $n3 = <>); print "Type in another number."; chomp(my $n4 = <>); print "Type in another number."; chomp(my $n5 = <>); print "$_\n" for sort { $a <=> $b } ($n1,$n2,$n3,$n4,$n5);
    I also added the use strict; use warnings;

    You should consider chomping in a loop into an array.
Re: How to make perl sort these values in order?
by ikegami (Patriarch) on Aug 07, 2011 at 04:30 UTC
    You're surely doing something wrong if you have many variables whose names end with a number.
    my @n; while (@n < 5) { if (@n) { print "Type in another number: "; } else { print "Type in a number: "; } chomp( my $n = <STDIN> ); push @n, $n; } print "$_\n" for sort { $a <=> $b } @n;

    Update: Added compare function as per reply.

      Or
      print "$_\n" for sort { $a <=> $b } @n;
      if you want the results sorted numerically rather than ASCIIbetically.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://919008]
Approved by muba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found