Dynamic variable assignment by recursion or loop?
Andreas Schultz
andreas.schultz@REDACTED
Mon Jul 6 08:51:07 CEST 2020
HI,
I prefer to keep the DNS labels as binaries, en/decoding them then becomes
a simple list/binary comprehension:
encode_fqdn(FQDN) ->
<< <<(size(Part)):8, Part/binary>> || Part <- FQDN >>.
decode_fqdn(FQDN) ->
[ Part || <<Len:8, Part:Len/bytes>> <= FQDN ].
Since you are working with 3GPP domains, you might be interested in more
examples for 3GPP data structures. Checkout gtplib [1] and pfcplib [2].
Regards
Andreas
[1]: https://github.com/travelping/gtplib
[2]: https://github.com/travelping/pfcplib
Am Fr., 3. Juli 2020 um 15:45 Uhr schrieb Papa Tana <papa.tana101@REDACTED
>:
> 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
> >
> >>
> >
>
--
Andreas Schultz
--
Principal Engineer
t: +49 391 819099-224
------------------------------- enabling your networks
-----------------------------
Travelping GmbH
Roentgenstraße 13
39108 Magdeburg
Germany
t: +49 391 819099-0
f: +49 391 819099-299
e: info@REDACTED
w: https://www.travelping.com/
Company registration: Amtsgericht Stendal
Managing Director: Holger Winkelmann
Reg. No.: HRB 10578
VAT ID: DE236673780
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200706/11ed10d1/attachment.htm>
More information about the erlang-questions
mailing list