473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Perl read in file and sort values

3
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;
}
Nov 18 '05 #1
2 18531
Niheel
2,460 Expert Mod 2GB
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.
Nov 18 '05 #2
mojju
3
Ok I have the following Perl code now... it doesnt print me out $min.. can anyone tell me why ?

Expand|Select|Wrap|Line Numbers
  1.  #! /usr/bin/perl 
  2.  
  3. #open file
  4. open(FILE,"gadzooks") or die("Unable to open file");
  5.  
  6. #read file into an array
  7.  
  8. @data = <FILE>;
  9.  
  10. $min = 10000;
  11. $max=0;
  12.  
  13.  
  14. while (<FILE>) {
  15. @data = <FILE>;
  16. @words = split( );
  17. foreach$n(@data){
  18. if($n<$min){
  19. $min = $n;
  20. print $min;
  21. }
  22. last;
  23. }
  24. }
  25.  
  26. #close file
  27. close(FILE);
  28.  

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.
Nov 19 '05 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
2
by: scadav | last post by:
I am new to Perl and I am trying for figure out how to solve this problem. If anyone can give me some suggestions, I would greatly appreciate it. I am trying to read a log file and generate...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
12
by: DaveMM | last post by:
I am a novice perl programmer, and I am trying to sort a hash by a value in the hash and I am having problems. I've done this before, but this time something is going wrong and I'm at a loss. I'm...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.