# Newbie questions on processing numerical data from text file

**URL:** <https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151>\
**Category:** Ruby\
**Created:** [June 29, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151 "2007-06-29T13:49:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/1 "2007-06-29T13:49:13Z")

</div>

Hi,

Hope you can help me get started with these trivial questions.  
I’ve got a practical problem, and the Ruby cookbook to help me, but a  
few combinations of the two haven’t produced any interesting results  
yet. I’d need a simple useful example to start with and then make my  
own variations.

Here is my practical problem:

- I open a text file, locate a line according to some keyword or line  
number, and change a value
- I run a fortran program
- I read a text file (output of this program), extract several  
numerical values and export them in a two column text file

The way I’ve done that so far is using bash commands such as ‘awk’,  
‘grep’, … but i need some loops and it’s getting too big for a  
sensible bash script.

Could you help me get started in Ruby with the simpler example below?

Here is the content of “sampleTextFile.dat”:

> other useless words …  
> I would like to read this data in some sort of array/hash, and export  
> the first column and the product of the last two columns to a new  
> file “results.dat”. This should be enough to understand the spirit I  
> guess, with the probably trivial problem to call the external program.

Any advice welcome.

Many thanks in advance!

baptiste

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/2 "2007-06-29T15:58:36Z")

</div>

How i create array under ruby?  
for example need create a vector type array

2007/6/29, Axel E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/3 "2007-06-29T16:03:36Z")

</div>

> How i create array under ruby?  
> for example need create a vector type array

Like this:

a=[]  
a=Array.new  
a=[1,2,3]  
a=[1,“a”,9.0]

You can include elements from all sorts of  
classes into an Array.

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/4 "2007-06-29T15:33:33Z")

</div>

Dear Baptiste,

welcome to Ruby!

> Here is my practical problem:
> 
> - I open a text file,

(… which already exists):

interesting\_text=IO.readlines(file\_name)

gives you an Array, with the lines as entries

> locate a line according to some keyword

interesting\_text.delete\_if{|line| /(r|R)uby/.match==nil}

removes all lines that don’t contain the string Ruby or ruby,

> or line number,

interesting\_text[line\_number] # (start counting at 0)

> and change a value  
> old\_value=1.2  
> new\_value=2.4

interesting\_text.sub!(value.to\_s,new\_value.to\_s)

> - I run a fortran program

result=`...` where … is the system command you use to start  
your fortran program,

> - I read a text file (output of this program), extract several  
> numerical values

You can do the reading with IO.readlines, the extraction with a  
Regexp,

> and export them in a two column text file

and this via

f=File.new(“my\_result\_file.txt”,“w”)  
f.puts formatted\_text  
f.close

> Here is the content of “sampleTextFile.dat”:
> 
> > this is a meaningless line  
> > another one
> > 
> > maybe this line acts as a keyword for the following data

I don’t know how to interpret the values in brackets in the  
following line:

> > 1 3.1 (415, 19e-12)

If you read the file in via IO.readlines, it’s a String element  
of an Array, say line=“1 3.1 (415, 19e-12)”  
so you could do:

numbers=line.gsub(/[(,)]/,’’).split \<= removes brackets an gives  
a four element Array (of Strings), which you can turn ito numbers via  
eval

number\_array=line.gsub(/[(,)]/,’’).split.map{|x| eval(x)}

Some other approach might be to assign variable names to each element  
in a line

vars\_names=[‘a’,‘b’,‘c’,‘d’]  
array\_of\_hashes=[]  
lines.each{|line|  
vals\_line=line.gsub(/[(,)]/,’’).split.map{|x| eval(x)}  
tmp={}  
vars\_names.each\_with\_index{|var\_name,index|  
tmp.store(var\_name,vals\_line[index])  
}  
array\_of\_hashes\<\<tmp  
}

will give something like:

[{“a”=\>1,“b”=\>3.1,“c”=\>415,“d”=\>19e-12},…]

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/5 "2007-06-29T19:47:57Z")

</div>

On 29 Jun 2007, at 14:32, Axel E. wrote:

> Dear Baptiste,
> 
> welcome to Ruby!

Thanks a lot!

I have a few problems though,

> > locate a line according to some keyword
> 
> interesting\_text.delete\_if{|line| /(r|R)uby/.match==nil}
> 
> removes all lines that don’t contain the string Ruby or ruby,

for some reason this doesn’t work for me. I get an error message:  
ArgumentError: wrong number of arguments (0 for 1)

if i replace this argument with something based on line numbers  
rather than regexp, it’s fine. Is the Regexp wrong?

> > and change a value  
> > old\_value=1.2  
> > new\_value=2.4
> 
> interesting\_text.sub!(old\_value.to\_s,new\_value.to\_s)

I can’t get that to work either :  
NoMethodError: private method `sub!’ called for #Array:0x7d180

> Best regards,
> 
> Axel

The rest works fine, thanks again!

baptiste

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/6 "2007-06-29T21:00:07Z")

</div>

> > interesting\_text.delete\_if{|line| /(r|R)uby/.match==nil}
> > 
> > removes all lines that don’t contain the string Ruby or ruby,
> 
> for some reason this doesn’t work for me. I get an error message:  
> ArgumentError: wrong number of arguments (0 for 1)
> 
> if i replace this argument with something based on line numbers  
> rather than regexp, it’s fine. Is the Regexp wrong?

You’re right. The line should read:

interesting\_text.delete\_if{|line| /(r|R)uby/.match(line)==nil}

> I can’t get that to work either :  
> NoMethodError: private method `sub!’ called for #Array:0x7d180

… and again … sub, gsub, sub!,gsub! are for Strings … so  
either:

interesting\_text=IO.readlines(file\_name).to\_s  
interesting\_text.sub!(old\_value.to\_s,new\_value.to\_s)

or , if, each line is to be processed separately,

interesting\_text=IO.readlines(file\_name)

new\_text=interesting\_text.each{|line|  
line.sub(old\_value.to\_s,new\_value.to\_s)}

Sorry for the confusion.

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 29, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/newbie-questions-on-processing-numerical-data-from-text-file/108151/7 "2007-06-29T22:34:21Z")

</div>

awesome, it works! It’s all starting to make some sense now.

Thanks!

baptiste
