<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
This doesn't work:<br>
<br>
<div style="color: #ffffff;background-color: #002451;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 16px;line-height: 22px;white-space: pre;"><div><span style="color: #ffffff;"> </span><span style="color: #ebbbff;">case</span><span style="color: #ffffff;"> </span><span style="color: #ffeead;">file</span><span style="color: #bbdaff;">:read_file(</span><span style="color: #ff9da4;">Fname</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;"> </span><span style="color: #ebbbff;">of</span></div><div><span style="color: #ffffff;"> {</span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Binary</span><span style="color: #ffffff;">} -> </span><span style="color: #ff9da4;">InputBuffer</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #ffeead;">binary</span><span style="color: #bbdaff;">:bin_to_list([</span><span style="color: #ff9da4;">Binary</span><span style="color: #bbdaff;">])</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #ffeead;">io</span><span style="color: #bbdaff;">:format(</span><span style="color: #d1f1a9;">"Character 4 is: </span><span style="color: #ffffff;">~p</span><span style="color: #d1f1a9;">"</span><span style="color: #bbdaff;">, [element(</span><span style="color: #ffc58f;">4</span><span style="color: #bbdaff;">, </span><span style="color: #ff9da4;">InputBuffer</span><span style="color: #bbdaff;">)])</span><span style="color: #ffffff;">,</span></div></div>
<br>
It prints all of the data, not just the fourth character.<br>
<br>
<div class="moz-cite-prefix">On 12/19/2018 8.22 PM, Dan Sommers
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:85a4ed40-eda3-794e-8b9f-45431e2d4463@potatochowder.com">On
12/19/18 6:04 PM, Donald Steven wrote:
<br>
<br>
> Taking your foo1,txt example, when I read it back
<br>
> (<a class="moz-txt-link-freetext" href="file:read_file(">file:read_file(</a>"foo1.txt"), how do I access the individual
characters
<br>
> of "Something I'll write to disk." That is, if
<br>
> <a class="moz-txt-link-freetext" href="file:read_file(">file:read_file(</a>"foo1.txt") yields {ok,<<"Something I'll
write to
<br>
> disk.">>}, how do I read the "S", then the "o", etc.?
<br>
<br>
What is your ultimate intent with all of those individual
characters?
<br>
<br>
(1) If you want to create a new list from them, then convert the
binary
<br>
to a list and use a list comprehension:
<br>
<br>
[operate_on_one_octet(Octet) || Octet <-
binary:bin_to_list(Binary)]
<br>
<br>
or perhaps lists:filter, lists:foldl, lists:map, etc.
<br>
<br>
(2) If you want to run some side-effect-producing function on each
one,
<br>
then use lists:foreach:
<br>
<br>
lists:foreach(fun print_one_octet(Octet) ->
<br>
io:format("--> ~p <--~n", [Octet])
<br>
end,
<br>
binary:bin_to_list([Binary]))
<br>
<br>
Dan
<br>
_______________________________________________
<br>
erlang-questions mailing list
<br>
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<br>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
<br>
</blockquote>
<br>
</body>
</html>