• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Trading Card Game 2 protagonist in the poll by clicking here.
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Question] Reading from file

  • 773
    Posts
    14
    Years
    • UK
    • Seen Dec 29, 2024
    I'm trying to read some stuff from file. The file is set up in the following format
    [SectionA]
    DisplayName=
    Name=
    [SectionB]
    DisplayName=
    Name=

    I'm able to read the file, but its not saving the section name(in the brackets) for the first section, every other detail and other section is fine. If i put the section in twice its fine

    This is the code
    Code:
     
    move = Move.new
    file = File.new("DataFiles/moves.txt", "r")
    while (line = file.gets)
        line.delete! "\n"
        line.delete! "\"\\"
        if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]
            section=$~[1] # The sections name contained in [x]
            if move.getInternalName != section
              ms.push(move)
              move = Move.new
            end
            move.setInternalName(section)
        else # Else of if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]
            split = line.split("=")
            if split[0] == "DisplayName"
              move.setDisplayName(split[1])
            end
        end
    end
    ms.push(move)
     

    I think its to do with the "if move.getInternalName != section" bit, but am unsure how to alter it.
    What i'm trying to do is when the section changes, push the move into the array, then recreate the move to use again
     
    Well... I don't use the files in that way because I don't like regular expresions...
    I prefer a file using CSV it's easier, in fact it is how many of the pbs files work.
    it would be something like:
    Section A,display name,name
    Section B,display name,name

    I would do the reading so:

    Code:
    file = open(link,"r+")
    lines = file.read
    lines = lines.split(/\n/)
    for line in lines
        split = line.split(",")
        #here you do whatever you want with the data
    end
     
    I had tried it that way round as well, but i get the same problem.
    The reason i had it done like that was because theres quite a few parts in each section, and not all the parts are required, that and it helps me remember what each bits for
     
    I tried
    Code:
      file = File.new("movestest.txt", "r")
      ms=[]
      move = []
      while (line = file.gets)
          line.delete! "\n"
          line.delete! "\"\\"
          if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]
              section=$~[1] # The sections name contained in [x]
              if move[0] != section
                ms.push(move)
                move = []
              end
              move[0]=section
          else # Else of if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]
              split = line.split("=")
              if split[0] == "DisplayName"
                move[1]=split[1]
              end
          end
      end
      ms.push(move)  
      p "ms",ms
    with file
    Code:
    [SectionA]
    DisplayName=Uno
    Name=Dos
    [SectionB]
    DisplayName=Tres
    Name=Cuatro
    And this produces:

    Code:
    "ms"
    [[], ["SectionA", "Uno"], ["SectionB", "Tres"]]

    The first "[]" is because of the logic at first 'ms.push(move)'. The text file is read correctly.
     
    After running this code (which is just a very quick MoveWrapper and your code with a small debug at the end):

    Code:
    class Move
      attr_accessor :internalName
      attr_accessor :displayName
      
      def setInternalName(name)
        @internalName = name
      end
      
      def getInternalName
        return @internalName
      end
      
      def setDisplayName(name)
        @displayName = name
      end
    end
    
    def testINI
      ms = []
      move = Move.new
      file = File.new("DataFiles/moves.txt", "r")
      while (line = file.gets)
          line.delete! "\n"
          line.delete! "\"\\"
          if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]
              section=$~[1] # The sections name contained in [x]
              if move.getInternalName != section
                ms.push(move)
                move = Move.new
              end
              move.setInternalName(section)
          else # Else of if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]
              split = line.split("=")
              if split[0] == "DisplayName"
                move.setDisplayName(split[1])
              end
          end
      end
      ms.push(move)
      
      for i in ms
        print i.getInternalName
      end
    end
    
    
    testINI

    with the Text File looking like this:

    Code:
    [SectionA]
    DisplayName=AA
    Name=BB
    [SectionB]
    DisplayName=CC
    Name=DD

    it returned (for the internal names):

    nil,
    SectionA,
    SectionB

    showing that the problem either with that first nil, or it's your move class itself. (I'm assuming it's the nil value) which can be removed from changing the line "if move.getInternalName != section" to "if move.getInternalName != section && move.getInternalName != nil" (or if the move's internal name is a blank string instead of putting a nil there put at blank string there)
     
    ok, that issue seems to have sorted itself out. Just ran through everything and its worked fine, though i'm now hitting another problem

    I have a class that loads in a text file and its made into an array (the array was previously saved using "save_data(moves,"Data/ms.dat")"

    Its being loaded in the class by
    Code:
    def initialize
        @ms=load_data("Data/ms.dat")
      end # End of def initialize

    and the class also has a get method for returning an item from the array
    Code:
    def getMS(value)
        a = -1
        for i in [email protected] - 1
          p "Wanted: #{value} Found: #{@ms[i].getInternalName}"
          if @ms[i].getInternalName == value
            a = @ms[i]
          end # End of if @ms[i].getInternalName == value
        end # End of for i in [email protected]
        return a
      end # End of def getMS(value)

    Now this is called as such from another class
    Code:
    m = ms.getMS(split[0])
    if m == -1
        raise "#{split[0]} not found"
    end # End of if a == -1

    But the getMS call is returning -1, and the message shown on screen when I print the "p "Wanted: #{value} Found: #{@ms.getInternalName}"" is
    "Wanted: SectionA Found: SectionA"
    though if i copy the message using ctrl-c its slightly different, it has a ? before the section name its found, which i believe is the issue.
    "Wanted: SectionA Found: ?SectionA"

    This doesn't happen though if i have it in the text file like so
    SectionA,display name,name
    SectionA,display name,name
    SectionB,display name,name
    If sectionA (or what ever is at the top) is in twice, its fine

    While this implies that theres a character being inserted at the beginning of the first line, i can't see where its coming from.
    The text files are made using notepad++ which doesn't hide characters, and as far as i know its not being inserted after the file being read
     
    ok, that issue seems to have sorted itself out. Just ran through everything and its worked fine, though i'm now hitting another problem

    I have a class that loads in a text file and its made into an array (the array was previously saved using "save_data(moves,"Data/ms.dat")"

    Its being loaded in the class by
    Code:
    def initialize
        @ms=load_data("Data/ms.dat")
      end # End of def initialize

    and the class also has a get method for returning an item from the array
    Code:
    def getMS(value)
        a = -1
        for i in [email protected] - 1
          p "Wanted: #{value} Found: #{@ms[i].getInternalName}"
          if @ms[i].getInternalName == value
            a = @ms[i]
          end # End of if @ms[i].getInternalName == value
        end # End of for i in [email protected]
        return a
      end # End of def getMS(value)

    Now this is called as such from another class
    Code:
    m = ms.getMS(split[0])
    if m == -1
        raise "#{split[0]} not found"
    end # End of if a == -1

    But the getMS call is returning -1, and the message shown on screen when I print the "p "Wanted: #{value} Found: #{@ms.getInternalName}"" is

    though if i copy the message using ctrl-c its slightly different, it has a ? before the section name its found, which i believe is the issue.


    This doesn't happen though if i have it in the text file like so

    If sectionA (or what ever is at the top) is in twice, its fine

    While this implies that theres a character being inserted at the beginning of the first line, i can't see where its coming from.
    The text files are made using notepad++ which doesn't hide characters, and as far as i know its not being inserted after the file being read
    Strange. A shot: Try copy and paste the txt content for a new file, so this may fix some metadata problems.

    And post the entire class/def or something that we can use for testing and solving your problem. As last resouce, just check and remove when the first character is "?" when reading a file.
     
    Back
    Top