[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

reading variables in a file

Ooo Grec

10/27/2006 1:31:00 PM

I'm thinking of migrating from Fortran to Ruby. Do you think i should go
ahead??
First problem i have.
How to read a file with several fields? This means:
Imagine a file:
Peter4 1990
Sam 3 1980
Grac6 1991

I would like to read for each line:name, order and year (3 variables per
line). How can i do this? and how can i write them into a file??
Thank you in advance.

--------------------------------------------------------------------------------

--
Posted via http://www.ruby-....

7 Answers

Farrel Lifson

10/27/2006 1:38:00 PM

0

On 27/10/06, Ooo Grec <ogrecio@gmail.com> wrote:
> I'm thinking of migrating from Fortran to Ruby. Do you think i should go
> ahead??
> First problem i have.
> How to read a file with several fields? This means:
> Imagine a file:
> Peter4 1990
> Sam 3 1980
> Grac6 1991
>
> I would like to read for each line:name, order and year (3 variables per
> line). How can i do this? and how can i write them into a file??
> Thank you in advance.
>
> --------------------------------------------------------------------------------
>
> --
> Posted via http://www.ruby-....
File.open(file) do |f|
f.each_line do |line|
name,order,year = *line.split
end
end

Farrel

Brad Tilley

10/27/2006 1:41:00 PM

0

Ooo Grec wrote:

> I would like to read for each line:name, order and year (3 variables per
> line). How can i do this?


String.split will work if there is whitespace in between each item:

irb(main):001:0> x = 'Peter 4 1990'
=> "Peter 4 1990"
irb(main):002:0> x.split
=> ["Peter", "4", "1990"]
irb(main):003:0> quit

Now, you have an array that contains [name, order, year]

--
Posted via http://www.ruby-....

naPOLeon

10/27/2006 1:44:00 PM

0

I would do this that way:

var = Array.new
File.open("file.txt", "r") do |file|
file.each_line do |line|
var << line.split
end
end

Now you have an array to play with.
If you want to access a specific line, use
sth = var[0] => ["Peter", "4", 1990"]

Or a single variable:

var[0][0] => "Peter"
var[2][2] => "1991"

If you want to write them into an other file, just write:

File.open("write.txt", "a+") do |file|
file << var
end

Hope thats, what you were looking for,

naPOLeon

Hugh Sasse

10/27/2006 1:59:00 PM

0

Chris Gernon

10/27/2006 2:01:00 PM

0

Ooo Grec wrote:
> I'm thinking of migrating from Fortran to Ruby. Do you think i should go ahead??

That entirely depends on what the application is, if you currently use a
number of Fortran libraries, etc. However, I say it never hurts to learn
another language (gives you another tool in your tool belt), and Ruby is
one of the easiest to learn and most useful out there.

> First problem i have.
> How to read a file with several fields? This means:
> Imagine a file:
> Peter4 1990
> Sam 3 1980
> Grac6 1991

A typical Ruby program to do this would look something like this. This
is pretty simple, so I combined reading from the file and writing to a
new file into the same program. Also, the real "Ruby Way" to do this
would be to create a Person class with name, order, and year attributes
... but I figured a "quick and dirty" approach of storing the values in
a hash would work for a simple example like this. (Note that # starts
comments, except inside double quotes, where #{} inserts a variable or
expression inside the double-quoted string). Hope this helps!

#!/usr/bin/env ruby -w

INPUT_FILE = 'data.txt'
OUTPUT_FILE = 'new_data.txt'

people = [] #empty array
File.open(INPUT_FILE) do |data| # open file for reading
data.each do |line| # for each line ...
# if line matches (text containing no digits, then 1 or more digits,
then a space, then 4 digits)
if line =~ /^(\D*)(\d+) (\d\d\d\d)$/
person = {:name => $1.strip, :order => $2, :year => $3} # capture
matched values in a hash
people << person # add the person hash to the people array
else
puts "Read line that was not in expected format!"
end
end
end
puts "read #{INPUT_FILE} file" # puts: put string (print to stdout)
puts 'Data read:'
people.each do |person| # for each person hash in the people array ...
puts "Name: #{person[:name]}, Order: #{person[:order]}, Year:
#{person[:year]}"
end

File.open(OUTPUT_FILE, 'w') do |new_data| # open file for writing
people.each do |person| # for each person hash in the people array ...
new_data.puts "#{person[:name]} #{person[:order]} #{person[:year]}"
# write string to file
end
end

--
Posted via http://www.ruby-....

American Eagle

3/12/2009 7:55:00 AM

0

B.H. Cramer wrote:
>
> "Gord McFee" <gord.mcfee@rogers.com> wrote in message
> news:TnEtl.33823$rp7.229@en-nntp-02.dc1.easynews.com...
>> On 3/10/2009 1:56 AM, American Eagle wrote:
>>
>>> Gord McFee wrote:
>>>> On 3/8/2009 10:19 PM, American Eagle wrote:
>>>>
>>>>> Gord McFee wrote:
>>>>>> On 3/8/2009 5:35 AM, American Eagle wrote:
>>>>>>
>>>>>>> Gord McFee wrote:
>>>>>>>> On 3/7/2009 7:35 PM, Patriot wrote:
>>>>>>>>
>>>>>>>>> Patrick Keenan wrote:
>>>>>>>>>
>>>>>>>>> I have one URL for a traitorous MF like you.
>>>>>>>>>
>>>>>>>>> ussliberty.com
>>>>>>>> Man, they sure don't like you assholes.
>>>>>>>>
>>>>>>>> <quote>
>>>>>>>>
>>>>>>>> The USS Liberty Memorial web site abhors the racist and extreme
>>>>>>>> positions taken by antiSemitic, Holocaust denial, conspiracy
>>>>>>>> theorist and other such groups which often seek to identify with
>>>>>>>> us and to usurp our story as their own. We have no connection
>>>>>>>> with and do not support or encourage support from any of these
>>>>>>>> groups including National Alliance, National Vanguard, The New
>>>>>>>> Order, National Socialists, The French Connection, Liberty
>>>>>>>> Lobby, American Free Press, Republic Broadcasting, AFP's Liberty
>>>>>>>> Radio Hour, or other such groups. We wish harm to no one and
>>>>>>>> encourage social justice and equality for everyone; we seek only
>>>>>>>> accountability for the criminal acts perpetrated against us and
>>>>>>>> can do that without help from hate-mongers.
>>>>>>> So that is why we all continue to sign their petitions to get
>>>>>>> justice. you are one lying sack of shit.
>>>>>> Before you slip further into incoherency, please note that all I
>>>>>> did was quote from the website *you* referred us to.
>>>>>>
>>>>>>> In fact you are a traitor that has stated that the survivors are
>>>>>>> liars.
>>>>>> I've done nothing of the kind, brainless one. Perhaps next time,
>>>>>> you will read a website before referring people to it.
>>>>>>
>>>>>> [incoherent rambling deleted]
>>>>> Try posting under the name you usually post under or no more
>>>>> responses.
>>>> This is the name I always post under, Don, because it is my name.
>>>
>>> Since you like crossposting so much, why don't you crosspost your
>>> anti USS Liberty bullshit to the Military groups? Try the name John.
>>
>> I have no idea what you are talking abut, Don.
>
> "Abut"? Is that a Canadian thing, eh?

Sure is.. Gordo is a Butt sniffer.
>
>
>
>>
>> --
>> Gord McFee
>> I'll write no line before its time
>>
>> Visit the Holocaust History Project
>> http://www.holocaust-h...
>>
>

B.H. Cramer

3/12/2009 8:51:00 AM

0


"American Eagle" <AE@USA.com> wrote in message
news:71rterFmqhc8U5@mid.individual.net...
> B.H. Cramer wrote:
>>
>> "Gord McFee" <gord.mcfee@rogers.com> wrote in message
>> news:TnEtl.33823$rp7.229@en-nntp-02.dc1.easynews.com...
>>> On 3/10/2009 1:56 AM, American Eagle wrote:
>>>
>>>> Gord McFee wrote:
>>>>> On 3/8/2009 10:19 PM, American Eagle wrote:
>>>>>
>>>>>> Gord McFee wrote:
>>>>>>> On 3/8/2009 5:35 AM, American Eagle wrote:
>>>>>>>
>>>>>>>> Gord McFee wrote:
>>>>>>>>> On 3/7/2009 7:35 PM, Patriot wrote:
>>>>>>>>>
>>>>>>>>>> Patrick Keenan wrote:
>>>>>>>>>>
>>>>>>>>>> I have one URL for a traitorous MF like you.
>>>>>>>>>>
>>>>>>>>>> ussliberty.com
>>>>>>>>> Man, they sure don't like you assholes.
>>>>>>>>>
>>>>>>>>> <quote>
>>>>>>>>>
>>>>>>>>> The USS Liberty Memorial web site abhors the racist and extreme
>>>>>>>>> positions taken by antiSemitic, Holocaust denial, conspiracy
>>>>>>>>> theorist and other such groups which often seek to identify with
>>>>>>>>> us and to usurp our story as their own. We have no connection with
>>>>>>>>> and do not support or encourage support from any of these groups
>>>>>>>>> including National Alliance, National Vanguard, The New Order,
>>>>>>>>> National Socialists, The French Connection, Liberty Lobby,
>>>>>>>>> American Free Press, Republic Broadcasting, AFP's Liberty Radio
>>>>>>>>> Hour, or other such groups. We wish harm to no one and encourage
>>>>>>>>> social justice and equality for everyone; we seek only
>>>>>>>>> accountability for the criminal acts perpetrated against us and
>>>>>>>>> can do that without help from hate-mongers.
>>>>>>>> So that is why we all continue to sign their petitions to get
>>>>>>>> justice. you are one lying sack of shit.
>>>>>>> Before you slip further into incoherency, please note that all I did
>>>>>>> was quote from the website *you* referred us to.
>>>>>>>
>>>>>>>> In fact you are a traitor that has stated that the survivors are
>>>>>>>> liars.
>>>>>>> I've done nothing of the kind, brainless one. Perhaps next time,
>>>>>>> you will read a website before referring people to it.
>>>>>>>
>>>>>>> [incoherent rambling deleted]
>>>>>> Try posting under the name you usually post under or no more
>>>>>> responses.
>>>>> This is the name I always post under, Don, because it is my name.
>>>>
>>>> Since you like crossposting so much, why don't you crosspost your anti
>>>> USS Liberty bullshit to the Military groups? Try the name John.
>>>
>>> I have no idea what you are talking abut, Don.
>>
>> "Abut"? Is that a Canadian thing, eh?
>
> Sure is.. Gordo is a Butt sniffer.

I doubt that very much, but it's nice to have a dig at the old feller when
he makes with the stuff ups.



>>
>>
>>
>>>
>>> --
>>> Gord McFee
>>> I'll write no line before its time
>>>
>>> Visit the Holocaust History Project
>>> http://www.holocaust-h...
>>>
>>
>