Perl read in file and sort values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mojju
    New Member
    • Nov 2005
    • 3

    Perl read in file and sort values

    Perl Experts:
    open(INFILE, $filename) or die("Cannot read input file: $!");

    #How can I use this while loop to scan/read numbers from three lines and #find the smallest number ?

    while ( <INFILE> ) {

    @words = split();

    last;
    }
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    Well you have the read in of the file part right.

    The @words=split(); part doesn't need to be there. That with the "Perl Experts:" pretty much gives away that you just copied the code and pasted it here, with no attempt to work on it.

    I will help you out though. Here is a simple algorithm with some links to help you along.
    1. Read file into an array
    2. Sort the data in the array - more
    3. Print specific array element. Output the first item in the array if your sorted from smallest to largest or out put the last item in an array if you sorted from largest to smallest.
    All the information you need to do this problem is there. The algorithm and the links to the proper information. Put the pieces together and see if it works.

    If you run into any problems, post your code here and we will help you out.
    niheel @ bytes

    Comment

    • mojju
      New Member
      • Nov 2005
      • 3

      #3
      Ok I have the following Perl code now... it doesnt print me out $min.. can anyone tell me why ?

      Code:
       #! /usr/bin/perl 
       
      #open file
      open(FILE,"gadzooks") or die("Unable to open file");
       
      #read file into an array
       
      @data = <FILE>;
       
      $min = 10000;
      $max=0;
       
       
      while (<FILE>) {
      @data = <FILE>;
      @words = split( );
      foreach$n(@data){
      if($n<$min){
      $min = $n;
      print $min;
      }
      last;
      }
      }
       
      #close file
      close(FILE);

      Originally posted by KUB365
      Well you have the read in of the file part right.

      The @words=split(); part doesn't need to be there. That with the "Perl Experts:" pretty much gives away that you just copied the code and pasted it here, with no attempt to work on it.

      I will help you out though. Here is a simple algorithm with some links to help you along.
      1. Read file into an array
      2. Sort the data in the array - more
      3. Print specific array element. Output the first item in the array if your sorted from smallest to largest or out put the last item in an array if you sorted from largest to smallest.
      All the information you need to do this problem is there. The algorithm and the links to the proper information. Put the pieces together and see if it works.

      If you run into any problems, post your code here and we will help you out.
      Last edited by Niheel; Nov 19 '05, 07:35 AM.

      Comment

      Working...