My code doesn’t compile. It seems that the problem lies in the main function with lookup.
I am trying to parse an file that look like this :
auiauiaui@tsrntsr.epep 584756 “srtrtte”
jepeto@pinokio.disney 464456454 “cendrillon”
If you name the file lol and the code prog then calling prog lol 1 2 will return 464456454.
EDIT :
Thanks,
See I have a file named filetest like this :
field1-1 field1-2 field1-3
field2-1 field2-2 field2-3
field3-1 field3-2 field3-3
field4-1 field4-2 field4-3
field5-1 field5-2 field5-3
Now my piece of code is suppose to grab a field given the line and the rank.
mycode filetest give me the number of lines in the file (so that i can use a script for each lines).
Here some example :
mycode filetest 1 2 -> field1-2
mycode filetest 2 2 -> field2-2
mycode filetest 3 4 -> field3-4
mycode filetest 3 2 -> field3-2
mycode filetest 2 5 -> field2-5
mycode filetest 1 4 -> field1-4
Now each field contain a String.
What i do is grab the file as a string then grab the line wanted (with lines) and then grab the field (with words).
I corrected my errors (thanks to the #haskell and #haskell-fr channel and you). But my code doesn’t work as expected at all.
Any help shall be appreciated. Here is the néw code :
import System.Environment
import System.IO
fieldArg :: Int -> [String] -> String
fieldArg x a = if length a > 2
then if x < 4 then a !! x else "field number is too high"
else "field number is too low"
lineArg :: Int -> [String] -> String
lineArg x a
| x > length a = "line number is too big"
| x < 1 = "line number is too low"
| otherwise = a !! x
extrcta :: Int -> Int -> String -> String
extrcta x n = fieldArg x . words . lineArg n . lines
argList :: String -> [((Int, String), String -> String)]
argList n = let n1 = read n
in [((1, n), extrcta 1 n1)
,((2, n), extrcta 2 n1)
,((3, n), extrcta 3 n1)
]
main :: IO ()
main = do
args <- getArgs
case args of
(path: opt1: opt2: _) -> case (read opt1, opt2) `lookup` argList opt2 of
Nothing -> putStrLn $ "Wrong argument " ++ opt1 ++ " or " ++ opt2
Just act -> withFile path ReadMode (\handle -> do
contents <- hGetContents handle
putStrLn $ act contents)
(path: _) -> withFile path ReadMode (\handle -> do
contents <- hGetContents handle
(print (length $ lines contents)))
_ -> putStrLn "Wrong number of argument"
EDIT2 : Thanks
The problem is in the 4th line of
main:opt1andopt2are strings butargListreturns a list with integer pairs. To fix this you could changeargListto:Now it compiles (but still does not seem to work as intended; don’t understand what your algorithm is supposed to do).
UPDATE corresponding to the updated question: That’s too complicated. Just drop that
argListthing and do something simple as: