Hey guys, I have a txt file that contain the following data: 34,1,2,3,3,1,6,5 55,3,3,1,1,2,5,6 66,2,4,4,4,3,3,3 67,4,5,5,5,6,2,2 68,5,1,2,2,4,4,4 69,6,6,6,6,5,1,1 where the numbers in position [0] are integers. so basically, i wan to sort the numbers from position [1..7] then pick the first four numbers after sorting in each line. any guidelines for how to do it. P.S I'm still a beginner in programming..just doing my exam project. Thank you for your help.
on 2012-11-28 15:16
on 2012-11-28 15:46
I haven't done any thing yet.. i just didn't know where to start. btw those numbers are not arrays. its the content of the file.
on 2012-11-28 15:49
read this: http://alvinalexander.com/blog/post/ruby/how-sort-... I tried to do something very similar, and after reading Alvin's blog understood exactly how to do it. Wayne ----- Original Message ---- From: Ismail M. <lists@ruby-forum.com> To: ruby-talk ML <ruby-talk@ruby-lang.org> Sent: Wed, November 28, 2012 8:20:20 AM Subject: sorting data from a file Hey guys, I have a txt file that contain the following data: 34,1,2,3,3,1,6,5 55,3,3,1,1,2,5,6 66,2,4,4,4,3,3,3 67,4,5,5,5,6,2,2 68,5,1,2,2,4,4,4 69,6,6,6,6,5,1,1 where the numbers in position [0] are integers. so basically, i wan to sort the numbers from position [1..7] then pick the first four numbers after sorting in each line. any guidelines for how to do it. P.S I'm still a beginner in programming..just doing my exam project. Thank you for your help.
on 2012-11-28 15:59
Ismail M. wrote in post #1086899: > I haven't done any thing yet.. i just didn't know where to start. What about parsing the file? I mean, we can help you with specific questions. But don't expect us to drag you to the solution or hand it out for you to copy and paste. You should at least have a vague idea of what is necessary to complete the task. The first thing is obviously to open/read the file. Can you do that? Ismail M. wrote in post #1086899: > btw those numbers are not arrays. its the content of the file. I do know that, but I was asking if you've already done the first step (no).
on 2012-11-28 16:09
On Wed, Nov 28, 2012 at 2:47 PM, Ismail M. <lists@ruby-forum.com> wrote: > I haven't done any thing yet.. i just didn't know where to start. > btw those numbers are not arrays. its the content of the file. Documentation for Ruby is here http://www.ruby-doc.org/core-1.9.3/ You're going to need to look at things like "File", possibly "Regexp", "Array", "String". Alan
on 2012-11-28 16:35
of course I see your point Jan E. I opened/read the file and displayed in on ruby command promt. then i thought of a way to sort the data in the file. l = [1,2,3,3,1,6,5] smallest = l.sort.first 4 => [1,1,2,3] however applying this method in my case is the problem. How do i connect this method to my file then make a loop than sort each line in my file.
on 2012-11-28 16:41
Read the blog entry I sent earlier. It will all make sense once you read it (you're going to want to store things into two different "fields" or objects stored in an array)... or start thinking about hashes. Wayne ----- Original Message ---- From: Ismail M. <lists@ruby-forum.com> To: ruby-talk ML <ruby-talk@ruby-lang.org> Sent: Wed, November 28, 2012 9:36:56 AM Subject: Re: sorting data from a file of course I see your point Jan E. I opened/read the file and displayed in on ruby command promt. then i thought of a way to sort the data in the file. l = [1,2,3,3,1,6,5] smallest = l.sort.first 4 => [1,1,2,3] however applying this method in my case is the problem. How do i connect this method to my file then make a loop than sort each line in my file.
on 2012-11-28 16:52
On Wed, Nov 28, 2012 at 3:36 PM, Ismail M. <lists@ruby-forum.com> wrote: > > => [1,1,2,3] > > however applying this method in my case is the problem. How do i connect > this method to my file then make a loop than sort each line in my file. Write a script "script.rb" and run it from the command line by typing "ruby script.rb". In your script, read the file into an array using myarray = IO.readlines("myfile.txt") Then you have to think about the elements of the array and look at the documentation to work out the rest. Alan
on 2012-11-28 20:15
Alan Forrester wrote in post #1086915: > > In your script, read the file into an array using > myarray = IO.readlines("myfile.txt") > Why would you suggest that? What's the matter with the method: IO.foreach(fname) {|line| #code here} > You're going to need to look at things like "File", possibly > "Regexp", No, File is unnecessary--even in your example, and the op said they are a beginner, so needlessly complicating the code with regexes is bad advice. String#split() is more than adequate.
on 2012-11-28 20:34
On 28 Nov 2012, at 19:15, 7stud -- <lists@ruby-forum.com> wrote: > Alan Forrester wrote in post #1086915: >> >> In your script, read the file into an array using >> myarray = IO.readlines("myfile.txt") >> > > Why would you suggest that? What's the matter with the method: > > IO.foreach(fname) {|line| #code here} I think the problem for somebody who knows very little Ruby will be filling in the "code here" part of that prescription without ever looking the object to which he is applying the code. Alan
on 2012-11-28 21:14
You can argue all day long about which solution is easier to understand and better for a beginner. But personally, I'm more interested in which methods *he* knows. If he knows File.readlines, that's fine. Then we can use it. If he knows File.foreach, that's fine, too. I know Ruby programmers love endless discussions about the best solution. But it would be great if maybe this time you could not do that and just help. @ Ismail: Please write down everything that you currently have (ideas, code, whatever). Your code snippet for the smallest number is already useful. You'll be able to use that later.
on 2012-11-29 10:08
It's beginner question. My advice is that you go through some beginners tutorial about Ruby. After that you will know to implement it. Here is your starting code: require 'csv' class ParseCSV def initialize @csv = CSV.read "data.csv" end def test @csv[0] # it's first row end end pc = ParseCSV.new pc.test As you see it's easier to process your data by csv class, since it's in comma separaterd values format. In constructor you initialize @csv variable which is 2 dimensional array. You acces it through indexes. For example to get 1st cell, then you type @csv[0][0]. You get first row by @csv[0]. If you want to go through all rows then you type: @csv.each do |row| 2012/11/28 Jan E. <lists@ruby-forum.com>
on 2012-11-29 10:08
If you want to go through all rows then you type: @csv.each do |row| # do something with row end
on 2012-11-30 10:55
Hello everybody!
i have a txt file which contains numbers as mentioned earlier..then i
made a method that shows the ranking rules.
def majority()
@majority = @rank[3]
if @rank[3] == @rank[4] then
@majority = @rank[4]
end
if @rank[4] == @rank[5] then
@majority = [5]
end
if @rank[5] == @rank[6] then
@majority = @rank[6]
end
so, how do i sort that file by the ones who has the lowest value of
@majority. i thought about using sort_by method..but i dont know how to
use it in my case.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.