Scala:读取和过滤行

时间:2015-11-04 08:13:35

标签: scala function filter

我目前正在开发一个Scala项目。作为第一次尝试处理这种困难的程序,我现在非常迷失和沮丧。

在这个项目中我应该:

  1. 读取一个名为map.txt的txt文件,其中包含许多文件 几个房间的信息
  2. 将特定行(部分)存储为两个不同的元素 数组(房间和动作),由两个定义的类组成。
  3. 文件中行的含义:

    • 3 在第一行是房间总数
    • 0 在第二行是第一个房间的room_number
    • Halsell 228 是第一个房间的房间名称
    • 段落是第一个房间的描述
    • 第四行中的
    • 1 是用户可以选择的不同方向的数量。这里是1,因为第一个房间的用户只能选择往东方
    • 第五行的
    • east 是用户可以选择的方向选项
    • 1 除了东方是选择往东方的目的地房间的房间号码
    • 那么房间1等等......

    所以基本上在这个 map.txt 中,我有3个房间:Halsell 228(1号房间),Halsell第2个走廊(2号房间),CS办公室(3号房间)

    我想我必须利用两个函数来过滤或取出我想要保存的部分行。但我真的不知道该怎么做。这就是我到目前为止所做的:

    import io.Source
    
    case class Action(direc: String, dest: Int)
    case class Room(roomNumber: Int, roomName: String, desc: String, array: Array[Action])
    
    val source = Source.fromFile("map.txt")
    val lines = source.getLines
    
    def parseLineR(line: String):Room = {
      // apparently IDK anything...
      Room(parts(0).toInt, parts(1),parts(2),parts(3))        
    }
    
    def parseLineD(line: String):Action = {
      // really don't know
      Action(parts(0),parts(1).toInt)                        
    }
    
    val room =      
    val actions = 
    
    source.close
    

    我想创建两个数组,它们可能如下所示:

    val rooms = Array(Room(0,"sa","sb",actions),Room(1,"ww","wa",actions),...)
    val actions = Array(Action("east", 0), Action("west", 1),...)
    

    每个数组中元素的数量应取决于

    1. 在房间里:是总房间数和
    2. in actions:是总方向选项的数量。

2 个答案:

答案 0 :(得分:0)

您的主要问题是行之间存在依赖关系和严格的顺序。源可能是在这里使用的错误类,因为它基本上假设您将读取几个相同格式的块。相反,请使用Java Scanner(您可以在Scala中使用,没有任何问题)。

扫描仪允许您一次获得一件事,因此您可以执行以下操作:

case class Action(direc:String, dest:Int)
case class Room(number:Int, name:String, desc:String, array:Seq[Action])

val sc = new java.util.Scanner(new File("map.txt"))

def readAction() = Action(sc.next(), sc.nextInt())

def readRoom() = Room(
  sc.nextInt(),
  sc.nextLine(),
  sc.nextLine(),
  for(i <- 1 to sc.nextInt()) yield readAction()
)

val roomCount = sc.nextInt()
val rooms = for(i <- 1 to roomCount) yield readRoom()
println(rooms)
sc.close()

打印:

Vector(
  Room(
    0, 
    Halsell 228,    
    You are standing in a room full of computers and comfy chairs with a giant air conditioning unit hanging from the ceiling.  While the surroundings are serine enough, you can't help feeling a certain amount of dread. This isn't just a fear that the air conditioning unit is going to fall either. Something in you tells you that this room is regularly used for strange rituals involving torture. You can only wonder what happens here and why there isn't blood all over the place. Your uneasyness makes you want to leave quickly.,Vector(Action(east,1))), Room(1, Halsell 2nd Hallway,    This is a long dark hallway with rough carpet and multiple doors leading out.  The dispair of lost souls hangs heavily in the air.,
    Vector(
        Action(west,0), 
        Action(east,2)
    )
  ), 
  Room(
     2, 
     CS Office, 
     You stand in the main office of the Computer Science Department at Trinity University.  An overwhelming evil fills the room.  It appears to come from one of the many side offices.  Looking at the door you see it says 201K.  You feel an overwhelming desire to flee this place before work is heaped upon you.,
     Vector(
       Action(west,1)
     )
   )
 )

(缩进矿)

请注意,您从文件中读取的顺序很重要。如果你说,改变构造函数中的属性顺序,你需要一些中间变量。

答案 1 :(得分:0)

这是一个使用尾递归,模式加工和避免可变性的解决方案:

        <td> <div class="field">
      <%= f.label :ad_author %><br>
        <!-- The field below is populated from AD (or perhaps the user table) if the user is logged in    -->
      <!-- \\TODO need ot add logic here in case the user isn't logged in.    -->
      <%= f.text_field :ad_author, :value => current_user.name, readonly => true %>
    </div></td>