0

I have question related Haskell language.i need to store bunch of characters in 2D array.How can i store it??I have characters in 10 X 10 format in text file and i want to store it in 2D character array in haskell language.Please help me as soon as possible..thank you..

Here is the code which i tried and in this code i am trying to store value of x in the list named listofchar::

module TreasureFile where
import System.IO  


main =  do 
    hdl <- openFile "map.txt" ReadMode
    readbychar hdl

readbychar hdl =  do  
                  t <- hIsEOF hdl
                  if t 
                    then return()                     
                    else do
                      let listofchar=[] 
                      x <- hGetChar hdl
                      if x =='\n' 
                        then putChar '!'--return()
                        else listofchar x
                      readbychar hdl
1
  • This question is similar to: 2 dimension array processing in haskell. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 31, 2024 at 7:18

1 Answer 1

2

Try this:

import System.IO

main = do
  textContents <- readFile "map.txt"
  let map = format textContents
  print $ map

format text = lines text

Lets step through this program:

First, readFile reads us the file and binds the contents to textContents.

Next we format the contents by splitting the list every time we encounter a newline delimiter and then remove the eventually remaining empty strings.

Done! Now we can do whatever we want with our "map".


A small note on the side:

It will seem strange that our map will be displayed like this:

["aaaaaaaaaa","bbbbbbbbbbb",..] -- doesn't look like 2D map

which is just syntatic sugar for:

[['a','a','a',..],['b','b','b',..],..] -- looks more like a map now
Sign up to request clarification or add additional context in comments.

1 Comment

i am getting this error When i tried it...Could not find module Data.List.Split' It is a member of the hidden package split-0.2.2'. Perhaps you need to add `split' to the build-depends in your .cabal file. Use -v to see a list of the files searched for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.