Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".

Papa Tana papa.tana101@REDACTED
Sun Jul 19 14:38:44 CEST 2020


Hi,

Thank you for interesting to my problem.

>> So in short: what are you *really* trying to do?

I'm building an application (experimental) with Erlang, which
communicates with a node called SGSN, using GTPv1 tunneling.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

     SGSN -----(GTPv1)-----> (my_App_Erlang)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

There are 255 kind of Message Type used in GTPv1:

 - Echo request
 - Echo response
 - Version Not Supported
 - Create PDP Context Request
 - Create PDP Context Response
 - reserved
 - PDU Notification Response
 - MBMS Session Update Response
 - ......
 - G-PDU

This week, I managed to connect successfully my App with a real SGSN,
but only some few messages are implemented in my code, because it will
take a lot of times to manage all type of message.
I will do it gradually.

My goal today is to optimize my code, and make it shorter as possible,
because when I find out List Comprehension this month (I just found
Erlang in 20th June, sorry), I change a piece of 50 lines code with
only 2 lines using your List and Binary Comprehension.

So now, as long as I am using Comprehension, my Code becomes more
shorter every day, it's pretty useful for me to read and maintain in
the future.

So, allow me to describe here my current problem:

One message I receive is a series of TLV (Tag, Length, Value) and
sometimes just TV (Tag Value), and of course not always has the same
format.

So, for this current question:

> T = [{a,1}, {length,24568}, {b,2}, {c,3}, {GSN_ADDRESS, {1,2,3,4}}, {GSN_ADDRESS, {5,6,7,8}].

I receive an Input REQ, I transform as T in response, some tag may
appears 2 times.
I am calculating the length of my new message(T), and I create a new
message ANSW:

Because T is very long serie of binary, I look something like:

> V = T but length = length(T), GSN_ADDRESS=myControlPlaneAddress, GSN_ADDRESS=MyUserPlaneAddress, myTag=MyValue......

I want to avoid to rewrite 2 times, and just copy all tags from a List
to another List but changes SOME VALUES only for SOME SPECIFIC TAGS
that I have processed.

Thanks,
Best Regards,


2020-07-19 14:59 UTC+03:00, Jesper Louis Andersen
<jesper.louis.andersen@REDACTED>:
> Hi!
>
> Your question looks like a typical X-Y problem, as described in:
> http://xyproblem.info/
>
> FWIW, your particular problem can be solved with list comprehensions, but
> require a helper function, as Oliver described:
>
> clamp_length({length, V}) when V > 255 -> {length, 255};
> clamp_length(Otherwise) -> Otherwise.
>
> t() ->
>     T = [{a,1}, {length,24568}, {b,2}, {c,3}, {length, 54741}],
>     [clamp_length(X) || X <- T].
>
> But I'm guessing this is not a good solution to your real problem. Chances
> are you are better off breaking your long list into smaller parts first,
> then process each subpart on its own. Generally, the structure of the data
> should be followed by your recursive descent, as you are really writing a
> parser here. As you get more special cases, one large function pattern
> match isn't going to cut it. If you split it up, it gets much easier to
> handle and the code will also become more readable if you think a bit about
> the naming of the functions.
>
> So in short: what are you *really* trying to do?
>
>
> On Sun, Jul 19, 2020 at 12:55 PM Papa Tana <papa.tana101@REDACTED> wrote:
>
>> I had to do it like below:
>>
>> T = [{a,1}, {length,24568}, {b,2}, {c,3}, {length, 54741}].
>>
>> WithoutLength = [ {Tag, Value} || {Tag, Value} <-T, Tag/= length].
>> HowManyLength = length([ {Tag, Value} || {Tag, Value} <-T, Tag==
>> length]).
>> Extra = lists:duplicate(HowManyLength, {length, 255}).
>>
>> >NewT = [WithoutLength | Extra ].
>> [[{a,1},{b,2},{c,3}],{length,255},{length,255}]
>>
>> Not really the expected result, yes all values of tag length are
>> replaced, but not in the right order.
>>
>
>
> --
> J.
>


More information about the erlang-questions mailing list