Lines in External Files

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
So I made a text file with this as a content:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.and used this script to make it work on RPGVXAce:

File.open("loremipsum.txt", "r") do |f| f.each_line do |line| puts line endendI stored a txt file called loremipsum on the game folder.

Now this is my question:

I have three attribute accessors called:

string1

string2

string3

Now. I want to detect which line I am reading on the text file, so I can do something like:

string1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"

string2 = "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "

string3 = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

So, I am in hopes for anyone to answer how I can detect the file lines so I can equate it with any variable, or even hp and mp.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
probably use an array?

Code:
text_array = []index = 0File.open("loremipsum.txt", "r") do |f|  f.each_line do |line|    text_array[index] = line    index += 1  endendp text_array[0] => Prints out line 1
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Ahhh! That solves it! Thank you very much! Why haven't I thought of that? :(
 

BoluBolu

Veteran
Veteran
Joined
Apr 24, 2014
Messages
452
Reaction score
117
Primarily Uses
You can also use instance_variable_set to set per line with an instance variable

File.open("loremipsum.txt", "r") do |f| f.each_line do |line| i += 1 instance_variable_set("@string#{i}", line.gsub(/\n/, "")) end end p @string1 p @string2 # and so on But I think this is not good, because why not use array? And that's miko have answer it for you. :)
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
This is something that I never asked yet on RGSS3, which is better multiple variables or arrays?


Back then on wc3, we avoid using arrays if possible as they're slower than multiple single variables.. as for RGSS3, IDK. but anyways, arrays are easier to work with IMHO.
 

BoluBolu

Veteran
Veteran
Joined
Apr 24, 2014
Messages
452
Reaction score
117
Primarily Uses
I don't know either, but having so many variables is(it's only my opinion)not good, don't know for other people. I just know that array is slow in ruby, and using hash is more suggested, but then we are same, I like messing things with array too XD(I don't know too why).
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
I actually expect hashes to be slower as arrays are simple integer indexes only, while hashes can take different types of keys..
 

BoluBolu

Veteran
Veteran
Joined
Apr 24, 2014
Messages
452
Reaction score
117
Primarily Uses
Last edited by a moderator:

FenixFyreX

Fire Deity
Veteran
Joined
Mar 1, 2012
Messages
434
Reaction score
310
First Language
English
Primarily Uses
Why not simply just

Code:
lines = File.read('some_doc.txt').split(/[\r\n]+/)
, or if you are working in VX or XP,
Code:
lines = File.open('some_doc.txt') {|f| f.read.split(/[\r\n]+/) }
@BoluBolu - Generally, if the array doesn't change at all (no adding or removing of objects), or it'll only be used very sparsely, it won't impact performance. In the C side of Ruby, an Array is simply a c array of long pointers to ruby objects (every ruby object in C is an address; a pointer, represented by a number address. This number is the __id__ of any object, or object_id, times 2). So, an array, as pure memory data, would look something like a huge string of numbers. The reason why array is slow is because every time the array adds an item, it has to check whether enough memory is allocated, and if not, allocate the entire array's worth and then add each item to the new array, plus the one you just added. This happens continually when you add and remove too many items, and thus impacts the performance.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Why not simply just

lines = File.read('some_doc.txt').split(/[\r\n]+/), or if you are working in VX or XP,
Code:
lines = File.open('some_doc.txt') {|f| f.read.split(/[\r\n]+/) }
@BoluBolu - Generally, if the array doesn't change at all (no adding or removing of objects), or it'll only be used very sparsely, it won't impact performance. In the C side of Ruby, an Array is simply a c array of long pointers to ruby objects (every ruby object in C is an address; a pointer, represented by a number address. This number is the __id__ of any object, or object_id, times 2). So, an array, as pure memory data, would look something like a huge string of numbers. The reason why array is slow is because every time the array adds an item, it has to check whether enough memory is allocated, and if not, allocate the entire array's worth and then add each item to the new array, plus the one you just added. This happens continually when you add and remove too many items, and thus impacts the performance.
So will lines be converted as a list or table of values? How will I know which line the lines can be accessed in? 
 

FenixFyreX

Fire Deity
Veteran
Joined
Mar 1, 2012
Messages
434
Reaction score
310
First Language
English
Primarily Uses
String#split returns an array, so yes; all lines will be returned as an array (list). It's one line, slightly quicker than adding another local variable, and easier to read.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Why not simply just


lines = File.read('some_doc.txt').split(/[\r\n]+/)
Never knew that before. :)
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I need to ask one more. See, I am going to try and use excel as a file or make tables. So if I would place Table A in excel as 1, 2, 3, 4, 5 downwards, is there a chance another possible way can be done to read tables in excel as opposed to lines in files? Or is this advanced enough?
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
They will be added, it's just that the script calls that are used above won't be able to read them inside the archive... Read the link that Hime posted for details.
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,853
Messages
1,016,990
Members
137,562
Latest member
tamedeathman
Top