<p dir="ltr">That wouldn't work for spaces in strings and binaries. You'd have to restrict it to /^\s+/.</p>
<div class="gmail_quote">Den 09/02/2014 00.02 skrev "Matthew Evans" <<a href="mailto:mattevans123@hotmail.com">mattevans123@hotmail.com</a>>:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div dir="ltr">The way I did it recently was (a bit convoluted):<div><br></div><div><div> LookingPrettierTerm = lists:flatten(io_lib:format("~p",[NastyTerm])),</div><div> LookingEvenPrettierTerm = re:replace(LookingPrettierTerm, "\\s+", "", [global,{return,list}]),</div>
<div> io:fwrite("Term looking pretty ~s~n",[<span style="font-size:12pt">LookingEvenPrettierTerm]),</span></div><div><br></div><br><div><hr>Date: Fri, 7 Feb 2014 14:49:29 -0800<br>From: <a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a><br>
To: <a href="mailto:a.brandon.clark@gmail.com" target="_blank">a.brandon.clark@gmail.com</a><br>CC: <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>Subject: Re: [erlang-questions] Erlang term to ASCII<br>
<br><div dir="ltr">Hashing of a string representation is probably not a great way to ensure that both systems agree on that structure. Even different versions of Erlang disagree on what exactly the representation of ~p should be for a given term, and I'm not talking about whitespace. It's simply not well suited for your purpose, even though it might appear to work for most of the cases you have now.<div>
<br></div><div>You could write your own Erlang implementation of whatever the CMS does, but you're unlikely to implement it correctly by taking "~p" and then trying to normalize the output.</div><div><br></div>
<div>You really should come up with a scheme to either hash the data structure itself (like Erlang's phash2 but portable to both sides of your system), or you should hash the output of a representation that has absolutely no flexibility in how data is encoded. Erlang's External Term Format is a lot closer, but there are still multiple valid ways to encode the same thing (particularly the atom cache table, using bigger encodings than necessary, etc.).</div>
</div><div><br><br><div>On Fri, Feb 7, 2014 at 2:16 PM, Brandon Clark <span dir="ltr"><<a href="mailto:a.brandon.clark@gmail.com" target="_blank">a.brandon.clark@gmail.com</a>></span> wrote:<br>
<blockquote style="border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">The non-Erlang app is an in-house configuration management system. It stores configuration settings in a SQL database and produces the app.config file used by my Erlang app on demand via REST interface; the configuration is delivered as a string in the body of an HTTP response.<div>
<br></div><div>My ultimate goal is to rework the Erlang app so that it fetches and applies most configuration updates automatically without restarting. For the near term, that means I have to cope with strings. In the long term, though, I have some sway over what the CMS does, provided I'm willing to get my hands dirty. ("Don't bring problems -- bring solutions.") I'm in favor of Erlang external terms and I think I can sell that solution with BERT and BERT-RPC.</div>
<div><br></div><div>Regardless of how we transmit the data, we still need a way to confirm that both systems agree on what the data structure contains. The current favored solution is to ask both systems to compute a hash of their data. We can normalize the data any way we want in preparation for hashing as long as both sides do it in exactly the same way. Since the CMS is already pretty good at building Erlang terms as strings without newlines or indentation, I set out to find a way to make Erlang do the same thing.</div>
<span><font color="#888888">
<div><br></div><div>~BC</div><div><br></div><div><br></div><div><div><br></div><div> </div></div></font></span></div><div><div><div><br><br><div>On Fri, Feb 7, 2014 at 11:16 AM, Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span> wrote:<br>
<blockquote style="border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">How is this other system maintaining the deeply-nested Erlang structure? How or why is it in text? How does it get rendered without newlines and indentation? Why not use a more predictable (less flexible) serialization format (erlang term format, JSON, protocol buffers, …)?<div>
<br></div><div>I do not think that this is the best approach. There are many possible representations of various tokens as Erlang source code and I would never trust two implementations to render it exactly the same way unless I wrote both of them. Some tokens that are going to be particularly problematic are lists of integers (which may or may not be rendered like strings, with various ways to escape), binaries (which may be rendered like strings or not), and floating point numbers.</div>
<div><br></div><div><br></div></div><div><br><br><div><div><div>On Fri, Feb 7, 2014 at 10:17 AM, Brandon Clark <span dir="ltr"><<a href="mailto:a.brandon.clark@gmail.com" target="_blank">a.brandon.clark@gmail.com</a>></span> wrote:<br>
</div></div><blockquote style="border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">I have 2 production systems, one Erlang and one not, both maintaining copies of a large, deeply-nested Erlang data structure. I need to set up a monitoring script to confirm that both systems are holding identical copies of the data structure.<div>
<br></div><div>The non-Erlang system is holding an ASCII rendering of the data structure. Computing an MD5 sum of this string is easy. If I can get the Erlang system to convert its data structure to a string, I can have it compute an MD5 sum as well and the monitoring is simply a matter of comparing hashes.</div>
<div><br></div><div>I'm stuck on the process of converting the Erlang term to a string.</div><div><br></div><div><font face="courier new, monospace">Str = io_lib:format("~p", [Data])</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="arial, helvetica, sans-serif">gives me what I want, except that it includes newlines and indentation that I can't expect the non-Erlang system to have.</font></div><div><font face="arial, helvetica, sans-serif"><br>
</font></div><div><font face="courier new, monospace">Str = io_lib:format("~w", [Data])</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">eliminates the newlines and indentation but renders the textual components of Data as lists of integers, guaranteeing the result won't match the non-Erlang system.</font></div>
<div><br></div><div>So the question is, how do I get a "~p"-style rendering of an arbitrarily-large Erlang term <i>without</i> newlines and indentation?</div><div><br></div><div>Thank you!</div><span><font color="#888888"><div>
<br></div><div>
~Brandon Clark</div><div><br></div></font></span></div>
<br></div></div><div>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
<br>_______________________________________________
erlang-questions mailing list
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a></div></div> </div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div>