Dynamic variable assignment by recursion or loop?

Papa Tana papa.tana101@REDACTED
Fri Jul 3 15:44:58 CEST 2020


For beginners like me, let me share here 3 steps to easily understand:

Input = ["TOPON", "S11", "NODE", "EPC", "MNC05", "MCC646",
"3GPPNETWORK", "ORG"].

> Wow = [ [string:len(X), X] || X <- Input ].

[[5,"TOPON"],
 [3,"S11"],
 [4,"NODE"],
 [3,"EPC"],
 [5,"MNC05"],
 [6,"MCC646"],
 [11,"3GPPNETWORK"],
 [3,"ORG"]]

> list_to_binary( Wow ).
<<5,84,79,80,79,78,3,83,49,49,4,78,79,68,69,3,69,80,67,5,
  77,78,67,48,53,6,77,67,67,...>>

Have a nice week-end All,
Best Regards,



2020-07-03 15:31 UTC+03:00, Papa Tana <papa.tana101@REDACTED>:
> @Hugo:
>  I wanted to escape binary initially and that's why I converted my
> Input as a List, and  worked with List and Tuple, but after many many
> fastidious lines with no valuable  result, I gave up.
>
> @Fernando:
>
>   You did it with ONLY ONE SINGLE LINE with your Binary comprehensions.
>   I still have definitely a lot to learn!!
>   Thanks a ton.
>
> Really appreciated,
>
>
> 2020-07-03 12:56 UTC+03:00, Fernando Benavides <elbrujohalcon@REDACTED>:
>> Or use binary comprehensions.
>> Something along the lines of this totally untested code…
>>
>> << << (length(Item)), (list_to_binary(Item))/binary >>  || Item <-
>> tuple_to_list(Input) >>.
>>
>> On Fri, Jul 3, 2020 at 11:48 AM Hugo Mills <hugo@REDACTED> wrote:
>>
>>> On Fri, Jul 03, 2020 at 10:43:22AM +0100, Hugo Mills wrote:
>>> >    There's a tuple_to_list/1 function. Use that to start with, and
>>> > then you can map over the list to generate a list of size/value
>>> > binaries, converting one at a time. Then finally fold over that list
>>> > to produce the final binary.
>>> >
>>> >    Alternatively, require the input to be a list in the first place,
>>> > and you can skip the tuple_to_list/1 part. (But the rest is still
>>> > necessary).
>>>
>>>    Sorry, just realised I should have added:
>>>
>>>    More fundamentally, work out how to do *one* thing (convert a
>>> string to a length+data binary), then use the list functions to
>>> map/filter/fold many of those into one result.
>>>
>>>    Occasionally, you'll have to write a recursive function directly,
>>> but it's rarer than you might think.
>>>
>>>    Hugo.
>>>
>>> --
>>> Hugo Mills             | Well, sir, the floor is yours. But remember,
>>> the
>>> hugo@REDACTED carfax.org.uk | roof is ours!
>>> http://carfax.org.uk/  |
>>> PGP: E2AB1DE4          |                                             The
>>> Goons
>>>
>>
>>
>> --
>> <https://about.me/elbrujohalcon?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=gmail_api&utm_content=thumb>
>> Brujo Benavides
>> about.me/elbrujohalcon
>> <https://about.me/elbrujohalcon?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=gmail_api&utm_content=thumb>
>>
>


More information about the erlang-questions mailing list