Alex Baban
Alex Baban

Reputation: 11732

How can I read values from a configuration file (text)?

I need to read values (text) from a configuration file named .env and assign them to variables so I can use them later in my program.

The .env file contains name/value pairs and looks something like this:

ENVIRONMENT_VARIABLE_ONE = AC9157847d72b1aa5370fdef36786863d9
ENVIRONMENT_VARIABLE_TWO = 73cad721b8cad6718d469acc42ffdb1f
ENVIRONMENT_VARIABLE_THREE = +13335557777

What I have tried so far

read-values.red

Red [

]

contents: read/lines %.env

env-one: first contents
env-two: second contents
env-three: third contents

print env-one ; ENVIRONMENT_VARIABLE_ONE = AC9157847d72b1aa5370fdef36786863d9
print env-two ; ENVIRONMENT_VARIABLE_ONE = 73cad721b8cad6718d469acc42ffdb1f
print env-three ; ENVIRONMENT_VARIABLE_ONE = +13335557777

What I'm looking for

print env-one ; AC9157847d72b1aa5370fdef36786863d9
print env-two ; 73cad721b8cad6718d469acc42ffdb1f
print env-three ; +13335557777

How do I continue or change my code and parse these strings such as the env- variables will contain just the values?

Upvotes: 1

Views: 307

Answers (2)

endo64
endo64

Reputation: 2307

Another solution using parse could be: foreach [word value] parse read %.env [collect some [keep to "=" skip keep to newline skip]] [set load word trim value]

This one will add the words to the global context ENVIRONMENT_VARIABLE_ONE will be AC9157847d72b1aa5370fdef36786863d9 and so on.

Upvotes: 3

giuliolunati
giuliolunati

Reputation: 766

env-one: skip find first contents " = " 3

See help for find and skip

Upvotes: 3

Related Questions