Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 874107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:03:12+00:00 2026-05-15T11:03:12+00:00

I have a simple record structure consisting of a header (H) and a list

  • 0

I have a simple record structure consisting of a header (H) and a list of the data lines (D) 1:N. All header lines must start with a digit. All data lines have a leading whitespace. There also might be some empty lines (E) in between that must be ignored.

L = [H, D, D, E, H, D, E, H, D, D, D].

I would like to create a list of records:

-record(posting,{header,data}).

using list comprehension. Whats the best way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:03:13+00:00Added an answer on May 15, 2026 at 11:03 am

    You should do something like this:

    make_records(L) when is_list(L) ->
      F = fun([32|_]=D,{#posting{}=H,Acc}) -> {H,[H#posting{data=D}|Acc]};
             ([], Acc) -> Acc;
             ([F|_]=H, {_,Acc}) when F=<$0, F>=$9 -> {#posting{header=>H}, Acc}
          end,
      {_, R} = lists:foldl(F, {undefined, []}, L),
      R.
    

    Anyway I think that straightforward Erlang version doesn’t seems too complicated and should be little bit faster.

    make_records2(L) when is_list(L) ->
      make_records2(L, undefined, []).
    
    make_records2([], _, R) -> R;
    make_records2([[32|_]=D|T], H, Acc) when is_list(H) ->
      make_records2(T, H, [#posting{header=H,data=D}|Acc]);
    make_records2([[]|T], H, Acc) ->
      make_records2(T, H, Acc);
    make_records2([[F|_]=H|T], _, Acc) when F>=$0, F=<$9 ->
      make_records2(T, H, Acc).
    

    Edit: If you have to add better row classification or parsing, adding new function is better because it improves readability.

    parse_row([Digit|_]=R) when Digit >= $0, Digit =< $9 -> {header, R};
    parse_row(R) -> try_spaces(R).
    
    try_spaces([]) -> empty;
    try_spaces([Sp|R]) when Sp=:=$\s; Sp=:=$\t; Sp=:=$\n ->
        try_spaces(R); % skip all white spaces from Data field
    try_spaces(Data) -> {data, Data}.
    

    You can use it like this:

    make_records(L) when is_list(L) ->
      F = fun(Row, {H, Acc}) ->
               case parse_row(Row) of
                 {data, D} when is_record(H, posting) -> {H,[H#posting{data=D}|Acc]};
                 empty -> Acc;
                 {header, H} -> {#posting{header=>H}, Acc}
          end,
      {_, R} = lists:foldl(F, {undefined, []}, L),
      R.
    

    Tail recursive native Erlang solution:

    make_records2(L) when is_list(L) ->
      make_records2([parse_row(R) || R<-L], undefined, []).
    
    make_records2([], _, R) -> R;
    make_records2([{data, D}|T], H, Acc) when is_list(H) ->
      make_records2(T, H, [#posting{header=H,data=D}|Acc]);
    make_records2([empty|T], H, Acc) ->
      make_records2(T, H, Acc);
    make_records2([{header,H}|T], _, Acc) ->
      make_records2(T, H, Acc).
    

    I think that there is no reason use tail recursion from performance point of view:

    make_records3(L) when is_list(L) ->
      make_records3(L, undefined).
    
    make_records3([], _) -> [];
    make_records3([R|T], H) ->
      case parse_row(R) of
        {data, D} when is_list(H) -> [#posting{head=H,data=D}|make_records3(T, H)];
        empty -> make_records3(T, H);
        {header, H2} -> make_records3(T, H2)
      end.
    

    … and many many other variants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.