[erlang-questions] Fw: Erlang n00b figuring out bins

Adrian Roe roe.adrian@REDACTED
Tue Sep 24 21:46:32 CEST 2013


Initial message failed - retrying  

-- 
Adrian Roe
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


Forwarded message:

> From: Adrian Roe <roe.adrian@REDACTED>
> To: jasingleton.work@REDACTED
> Cc: erlang-programming@REDACTED
> Date: Tuesday, 24 September 2013 20:44:41
> Subject: Re: [erlang-questions] Erlang n00b figuring out bins
> 
> The first thing I'd say is to ask why you want a list of strings (lists) - I initially went down the strings are lists route but am definitely in the strings as binaries camp now, as are an increasing number of Erlang projects.  Definitely a question of style, but the style du jour appears to be binaries. Also the example you give doesn't run for me (as I'd expect) - binary_to_list( [<<"s">>, <<"t">>, <<"u">>, <<"f">>, <<"f">>]) is trying to convert a list of binaries rather than a binary which is what it expects
> 
> Eshell V5.10.1  (abort with ^G)
> 1> binary_to_list( [<<"s">>, <<"t">>, <<"u">>, <<"f">>, <<"f">>]).
> ** exception error: bad argument
>      in function  binary_to_list/1
>         called as binary_to_list([<<"s">>,<<"t">>,<<"u">>,<<"f">>,<<"f">>])
> 
> 
> That aside there are two jobs going on there:
> You need to split the input on "."
> You want the output as a list of strings (= list of lists)
> 
> You can go about that in (at least) two different ways
> 
> Split the binary and convert the binary parts into strings
> Convert the input to a list and split it
> 
> Thankfully there are library calls that do just what you need.  Lets look at splitting then converting - binary:split/2 looks like it does the job
> 
> Eshell V5.10.1  (abort with ^G)
> 1> X = <<"stuff.junk.excess">>.
> <<"stuff.junk.excess">>
> 2> binary:split(X, [<<".">>]).
> [<<"stuff">>,<<"junk.excess">>]
> 
> 
> Close, but not quite what we want as it has only split on the first occurrence.  Thankfully there's binary:split/3 which gives us a little more control
> 
> 3> binary:split(X, [<<".">>], [global]).
> [<<"stuff">>,<<"junk">>,<<"excess">>]
> 
> 
> Perfect - now we just need to convert the binary strings into lists.  Maybe something like
> 
> 4> [binary_to_list(Y) || Y <- binary:split(X, [<<".">>], [global])].
> ["stuff","junk","excess"]
> 
> 
> Again with the other approach (convert then split), there's a function that does what we need - string:tokens/2.  This expects a string (aka list) as input
> 
> 5> string:tokens(binary_to_list(X), ".").
> ["stuff","junk","excess"]
> 
> 
> Not sure if any of that counts as "more graceful" but more widely you should definitely stick with it - Erlang is certainly worth the initial head-scratching.
> 
> Adrian
> -- 
> 
> Dr Adrian Roe
> Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
> 
> 
> On Tuesday, 24 September 2013 at 15:46, jasingleton.work@REDACTED (mailto:jasingleton.work@REDACTED) wrote:
> 
> > I've done a lot of development, but it's been quite a while since I've done anything
> > functional.  Erlang is still brand-new for me, so be warned. 
> > 
> > I've got a (conceptual) string arriving in my sw as a binary, e.g. <<"stuff.junk.excess">>
> > and trying to transform it into a list of strings, e.g. ["stuff", "junk", "excess"]
> > The mechanism I'm using is giving me a list of lists of binaries, e.g.
> >  [ [<<"s">>, <<"t">>, <<"u">>, <<"f">>, <<"f">>], [<<"j">>, <<"u">>   (etc)
> > 
> > I ended up hacking it to just take each of the lists of binaries and run it through
> > list_to_binary and then binary to list.  The result is what I really wanted, which is 
> >     list_to_binary(binary_to_list( [<<"s">>, <<"t">>, <<"u">>, <<"f">>, <<"f">>]))
> > gives me back
> >     "stuff"
> > 
> > That's all well and good, and functional, but has two problems:
> > 1) I honestly don't get why it worked; I would expect those two operations to
> >     give me back something unchanged.
> > 2) There's got to be a more graceful way to achieve this.
> > 
> > Thoughts?
> > 
> > 
> > 
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED (mailto:erlang-questions@REDACTED)
> > http://erlang.org/mailman/listinfo/erlang-questions
> > 
> > 
> > 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130924/4767e4e5/attachment.htm>


More information about the erlang-questions mailing list