Message Boards Message Boards

1
|
4349 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How do I preserve unevaluated input when writing a list of rules to a file?

Posted 11 years ago
How do I prevent this from evaluating when writing it out to a file?
data = {
        "key1" -> Foo /@ {"val11", "val12"},
        "key2" -> Foo /@ {"val21", "val22"},
         ...
       } 

After processing data (changing "val**" in some way), I'd like to write it to file while preserving the form "Foo /@ ". It writes it out in the evaluated form:
{Foo["val11"], Foo["val12"]}

I usually use
ToString[expression, InputForm, CharacterEncoding -> "ASCII"] 

and WriteString[] to write expressions to file. But this breaks the "Foo /@ ".

Any suggestions?
POSTED BY: Meng Lu
2 Replies
In[25]:= ToString[Unevaluated[foo /@ {1, 2}], InputForm, 
          CharacterEncoding -> "ASCII"] 
Out[25]= "foo /@ {1, 2}" 
POSTED BY: Jeremy Michelson
Hello Meng,
Does this do what you are looking for?
data = {"key1" -> Defer[Foo /@ {"val11", "val12"}],
  "key2" -> Defer[Foo /@ {"val21", "val22"}]}

Export["temp.m", data]

newdata = << temp.m
POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract