how to read file and skip some white spaces

[亡魂溺海] 提交于 2019-12-24 00:48:57

问题


this is similiar to my previous question,

but there is another improvisation, how is the code if i want to skip some white spaces, for this case is "enter", for example:

5 5 10
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
               <- this white space
3 4 4
1 2 3 4
1 2 3 4
1 2 3 4

i try to my best but couldn't find how to skip the white space thank you for the help :)

This is the answer, thanks to Ramon :

let readMap (path:string) =
    let lines = File.ReadAllLines path
    let [|x; y; n|] = lines.[0].Split() |> Array.map int
    let data = 
        [| 
            for l in (lines |> Array.toSeq |> Seq.skip 1 |> Seq.filter(System.String.IsNullOrEmpty >> not)) do
                yield l.Split()
        |]
    x,y,n,data

回答1:


Another way to write your readMap function is to use if expression inside the list comprehension. I think this is actually more readable if you're using comprehensions (because you don't have to combine two ways of writing things):

let readMap (path:string) =
    let lines = File.ReadAllLines path
    let [|x; y; n|] = lines.[0].Split() |> Array.map int
    let data = 
        [| 
            for l in lines |> Seq.skip 1 do
                if not (System.String.IsNullOrEmpty(l)) then
                    yield l.Split()
        |]
    x,y,n,data

I also removed the call to Array.toSeq, because F# allows you to use array in a place where seq is expected without an explicit conversion (seq is actually IEnumerable and array implements it).




回答2:


What about this:

val items : string list
items
|> List.filter (System.String.IsNullOrEmpty >> not)

?



来源:https://stackoverflow.com/questions/7677265/how-to-read-file-and-skip-some-white-spaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!