Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7052081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:20:27+00:00 2026-05-28T03:20:27+00:00

I have a Java like property text files with key value pairs. What are

  • 0

I have a Java like property text files with key value pairs. What are some good approaches for loading that data into haskell and then accessing it.

The file look likes:

XXXX=vvvvv   
YYYY=uuuuu

I want to be able to access the “XXXX” key.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:20:28+00:00Added an answer on May 28, 2026 at 3:20 am

    Exploring a few details ehird didn’t mention, and a slightly different approach:

    import qualified Data.Map as Map
    
    type Key = String
    type Val = String
    
    main = do
      -- get contents of file (contents :: String)
      contents <- readFile "config.txt"
    
      -- split into lines (optionList :: [String])
      let optionList = lines contents
    
      -- parse into map (optionMap :: Map Key Val)
      let optionMap = optionMapFromList optionList
    
      doStuffWith optionMap
    
    optionMapFromList :: [String] -> Map.Map Key Val
    optionMapFromList = foldr step Map.empty
      where step line map = case parseOpt line of
              Just (key, val) -> Map.insert key val map
              Nothing         -> map
    
    parseOpt :: String -> Maybe (Key, Val)
    parseOpt = undefined
    

    I’ve expressed my solution to your problem as a fold: taking the list of lines in the file, and turning it into the desired map. Each step of the fold involves inspecting a single line, attempting to parse it into a key/value pair, and when successful, inserting it into the map.

    I’ve left parseOpt undefined; you could use an approach like ehird’s parseField, or whatever you like. Perhaps you would prefer to only parse specific options:

    interestingOpts = ["XXXX", "YYYY"]
    
    parseOpt line = case find (`isPrefixOf` line) interestingOpts of
      Just key -> Just (key, drop 1 $ dropWhile (/= '=') line)
      Nothing  -> Nothing
    

    Using the prefix testing approach isn’t always the best idea, though, if you have (for example) an option “XX” and an option “XXXX”. Play around and see what approach suits your data best. If you need high performance, look into using Data.Text instead of Strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my jsp, I have some fields like this : <html:text property=field[0] value=${fieldValue} indexed=true>
First I would like to say that I have only worked with java for
I have a Java properties file defined as something like: Property.1=value1 Property.2=value2 ... There
I have a Java problem that involves reading a text file and check that
I have a Java file TestThis.java like the following: class A { public void
So I have a java hashmap like below: hMap.put(1, One); hMap.put(2, Two); hMap.put(3, Two);
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I have mapped several java classes like Customer, Assessment, Rating, ... to a database
As a matter of fact you have smth like /java/src and /java/test . But
I'd like to have a java.utils.Timer with a resettable time in java.I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.