Lines in External Files

● ARCHIVED · READ-ONLY
Started by Milena 16 posts View original ↗
  1. 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.
  2. 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
  3. Ahhh! That solves it! Thank you very much! Why haven't I thought of that? :(
  4. 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. :)
  5. 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.
  6. 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).
  7. I actually expect hashes to be slower as arrays are simple integer indexes only, while hashes can take different types of keys..
  8. Well I just know from several site and peoples told me that hash are more faster for lookup things, that's what many people told me.

    Try look at this for instance :

    http://stackoverflow.com/questions/5551168/performance-of-arrays-and-hashes-in-ruby

    EDIT : But yeah I don't really know what's happen in there. XD

    Err.. Alright, our discussion is already OOT, I'm afraid a mod will not like it. Haha so I'm out.
  9. 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.
  10. FenixFyreX said:
    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? 
  11. 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.
  12. Why not simply just


    lines = File.read('some_doc.txt').split(/[\r\n]+/)
    Never knew that before. :)
  13. 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?
  14. Tsukihime said:
    Note that if you are planning to encrypt your data, the script won't work.
    Does that mean whenever I compile my project and placed the files on the data folder, it won't be added? Or I just have to change the file extension to be used?
  15. 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.