Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > please help load from txt

Reply
Thread Tools

please help load from txt

 
 
Lark Work
Guest
Posts: n/a
 
      11-15-2010
hi i new to this forum and i have a problem a made a script containing a
load data from a file but i want it to load one line at the time the
data from that line must be used but i don't know how i (and maybe if
its possible i want to call a line with gets.chomp of something) so can
u please help me this is my code :

class Class_load
def initialize(textfile)
@text = IO.readlines(textfile)
@data = []
for i in 0...@text.size
@text[i].each(',') {|s| @data.push(s.delete(","))}
end
show_load_data
end
def load_data
end
def show_load_data
puts "Name: #{@data[0]} #{@data[1]}"
puts "Callname: #{@data[2]}"
puts "Age: #{@data[3]}"
end
end

Class_load.new("data.txt")


and this is how my data.txt looks like:

name,lastname,callname,age
name2,lastname2,callname2,age2
etc

so can somebody please help me????

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

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-15-2010
On Mon, Nov 15, 2010 at 9:32 AM, Lark Work <> wrote=
:
> hi i new to this forum and i have a problem a made a script containing a
> load data from a file but i want it to load one line at the time the
> data from that line must be used but i don't know how i (and maybe if
> its possible i want to call a line with gets.chomp of something) so can
> u please help me this is my code :
>
> class Class_load
> =A0def initialize(textfile)
> =A0 =A0@text =3D IO.readlines(textfile)
> =A0 =A0@data =3D []
> =A0 =A0for i in 0...@text.size
> =A0 =A0 =A0@text[i].each(',') {|s| @data.push(s.delete(","))}
> =A0 =A0end
> =A0 =A0show_load_data
> =A0end
> =A0def load_data
> =A0end
> =A0def show_load_data
> =A0 =A0puts "Name: #{@data[0]} #{@data[1]}"
> =A0 =A0puts "Callname: #{@data[2]}"
> =A0 =A0puts "Age: #{@data[3]}"
> =A0end
> end
>
> Class_load.new("data.txt")
>
>
> and this is how my data.txt looks like:
>
> name,lastname,callname,age
> name2,lastname2,callname2,age2
> etc
>
> so can somebody please help me????


Depending on what you want to do with the data after reading it, if
it's just printing it to the screen I would do something like this:

irb(main):009:0> File.foreach("data.txt") do |line|
irb(main):010:1* name,last_name,call_name, age =3D line.chomp.split(",")
irb(main):011:1> p [name,last_name,call_name, age]
irb(main):012:1> end
["name", "lastname", "callname", "age"]
["name2", "lastname2", "callname2", "age2"]


(substitue line 11 with your printing logic). If you want to store the
data in memory to process later, you could use an array of Struct or
something like that, for example:

Person =3D Struct.new :name,:last_name,:call_name,:age

class PersonList
attr_reader ersons

def initialize file
@persons =3D []
File.foreach("data.txt") do |line|
@persons << Person.new(line.chomp.split(","))
end
end
end

person_list =3D PersonList.new("data.txt")
p person_list # do something with person_list.persons

Jesus.

 
Reply With Quote
 
 
 
 
Lark Work
Guest
Posts: n/a
 
      11-15-2010
hi thanks for your reply i going to try it what i want is that is just
prints it in

puts "Name: #{@data[0]} #{@data[1]}"
puts "Callname: #{@data[2]}"
puts "Age: #{@data[3]}"

data0 = name
data1 = lastname
data2 = callname
and data3 = age

and thats what i want to call from the txt file

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

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-15-2010
On Mon, Nov 15, 2010 at 10:19 AM, Lark Work <> wrot=
e:
> hi thanks for your reply i going to try it what i want is that is just
> prints it in
>
> =A0 =A0puts "Name: #{@data[0]} #{@data[1]}"
> =A0 =A0puts "Callname: #{@data[2]}"
> =A0 =A0puts "Age: #{@data[3]}"
>
> data0 =3D name
> data1 =3D lastname
> data2 =3D callname
> and data3 =3D age
>
> and thats what i want to call from the txt file


If you just want to print it, I wouldn't bother with classes or
anything more complex:

File.foreach("data.txt") do |line|
name,last_name,call_name, age =3D line.chomp.split(",")
puts "Name: #{name} #{last_name}"
puts "Callname: #{call_name}"
puts "Age #{age}"
end

This should suffice.

Jesus.

 
Reply With Quote
 
Lark Work
Guest
Posts: n/a
 
      11-15-2010
thanks for your help, but i got that already i don't think you quit
understand me because i want to call just one line of at the time this
is how data.txt looks like

hisname,lastname,callname,age
otherperson,lastname,callname,age
etc

i wan't it to work like a a database of datas of persons and i want to
call one person at the time is that posible???


thanks,


lark

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

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-15-2010
On Mon, Nov 15, 2010 at 7:47 PM, Lark Work <> wrote:
> thanks for your help, but i got that already i don't think you quit
> understand me


Sorry about that. Let's try to move forward then:

> because i want to call just one line of at the time this
> is how data.txt looks like


I have problems understanding what you mean by "call".

> hisname,lastname,callname,age
> otherperson,lastname,callname,age
> etc
>
> i wan't it to work like a a database of datas of persons and i want to
> call one person at the time is that posible???


Here I don't understand what you mean by "call" and "at the time". If
you want it to work like a database of persons, then you would like to
load the data into a data structure that supports containing the list
of persons, and querying by some key, for example, the name. Is that
something that makes sense? I don't have time right now, but I'll come
back later with an example of what I mean.

Jesus.

>
>
> thanks,
>
>
> lark
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-15-2010
2010/11/15 Jes=FAs Gabriel y Gal=E1n <>:
> On Mon, Nov 15, 2010 at 7:47 PM, Lark Work <> wro=

te:
>> thanks for your help, but i got that already i don't think you quit
>> understand me

>
> Sorry about that. Let's try to move forward then:
>
>> =A0because i want to call just one line of at the time this
>> is how data.txt looks like

>
> I have problems understanding what you mean by "call".
>
>> hisname,lastname,callname,age
>> otherperson,lastname,callname,age
>> etc
>>
>> i wan't it to work like a a database of datas of persons and i want to
>> call one person at the time is that posible???

>
> Here I don't understand what you mean by "call" and "at the time". If
> you want it to work like a database of persons, then you would like to
> load the data into a data structure that supports containing the list
> of persons, and querying by some key, for example, the name. Is that
> something that makes sense? I don't have time right now, but I'll come
> back later with an example of what I mean.


Got a couple of minutes:

Person =3D Struct.new :name,:last_name,:call_name,:age

class PersonList
attr_reader ersons

def initialize file
@persons =3D {}
File.foreach(file) do |line|
name, *rest_of_data =3D line.chomp.split(",")
@persons[name] =3D Person.new(name, *rest_of_data)
end
end
end

person_list =3D PersonList.new("data2.txt")
p person_list.persons["name"]

I tried with this file:

name,lastname,callname,age
name2,lastname2,callname2,age2


and got:

$ ruby persons.rb
#<struct Person name=3D"name", last_name=3D"lastname",
call_name=3D"callname", age=3D"age">


Jesus.

 
Reply With Quote
 
Lark Work
Guest
Posts: n/a
 
      11-15-2010
he jesus thanks for your help by call i meant, when the scripts start i
can choose from database when i do that i can type in a name of who i
wan't to show. i hope i am making my self clear now (i am 15 years old
and not english so my english isn't so good).

i will try it thanks again i realy apricate it


lark

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

 
Reply With Quote
 
Lark Work
Guest
Posts: n/a
 
      11-15-2010
heey if that last script that you maked is what i need?? how do i put it
toghteter i am trying buts its gives my a lot of errors and like a said
i am not so old and don't have many experience

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

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-15-2010
On Mon, Nov 15, 2010 at 8:58 PM, Lark Work <> wrote:
> heey if that last script that you maked is what i need?? how do i put it
> toghteter i am trying buts its gives my a lot of errors and like a said
> i am not so old and don't have many experience


What errors did you get? I tried with your sample file and got it working.
In any case, what is left is to use gets to have the user type a name
and use that to look in the hash (instead of the fixed "name" I
wrote).

Jesus.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
How to load a CSV/TXT file in a textarea? =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= HTML 11 03-25-2006 08:47 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57