How to pick up numbers from a text ?

For developers writing C++, Fortran, Java, code who have questions or comments to make.

Moderators: silvia, selimgunay, Moderators

Post Reply
lsfhan
Posts: 3
Joined: Thu Jun 20, 2013 4:56 am

How to pick up numbers from a text ?

Post by lsfhan »

I want to pick up some numbers from a text,such as file.txt .At the same time I set a(n) ,the range of n is from one to the largest number of the numbers ,and per a(n) equal one number of the text .for example a(1)=2,a(2)=4,a(3)=5,.........2,4,5 are the numbers from the file.txt
thanks for your interpretation!
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: How to pick up numbers from a text ?

Post by fmk »

set inFile [open data.txt r]
set numbers [list]
while { [gets $inFile line] >= 0 } {
foreach word [split $line] {
lappend numbers $word
}
}
puts $numbers


note the list indexing starts at 0 and not 1
lsfhan
Posts: 3
Joined: Thu Jun 20, 2013 4:56 am

Re: How to pick up numbers from a text ?

Post by lsfhan »

Thank you for answering. I have understood which you had provided to me .
But I expect to set a(n), such as
for {set n 1} {$n<=10} {incr n 1} {
set a([expr $n]) $numbers
puts "a([expr $n]):$a([expr $n])"
}
I would like to applicate a(n) to solve the problem .Such as d(n)=a(n)+n*n .
But I can't get the real result that I expect .IF I input these procedures into opensees
,I obtain a(1)=a(2)=..............=a(10)=all numbers from data.txt .Rather than a(1)=-1.4,a(2)=-10.8......a(10)=-8.5
here is the data.txt
"-1.4
-10.8
-10.1
-8.8
-9.5
-12
-14.2
-12.8
-11
-8.5"
Thank you again for answering
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: How to pick up numbers from a text ?

Post by fmk »

i have not a clue as to what you want to do. could you rephrase.
lsfhan
Posts: 3
Joined: Thu Jun 20, 2013 4:56 am

Re: How to pick up numbers from a text ?

Post by lsfhan »

Sorry ,I hadn't explained the problem clearly .
I want to use central difference method to resolve the Equation of Motion .a(n ) is earthquake acceleration
which is equal to numbers from data.txt. I must input one a(n) per step of numerical calculation .M is mass and -M*a(n)
as external force. I expect to know how to pick up numbers from data.txt . n is from 1 to the maximum of the numbers from data.txt.
Thank you very much
Post Reply