开发者

how to read file and skip some white spaces

开发者 https://www.devze.com 2023-04-11 10:46 出处:网络
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:

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


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).


What about this:

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

?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号