# Lines of code in modules -- without comments and with conditional compile applied

**URL:** <https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726>\
**Category:** Help!\
**Created:** [March 17, 2021, 10:06am UTC](https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726 "2021-03-17T10:06:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 17, 2021, 10:06am UTC](https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726/1 "2021-03-17T10:06:10Z")

</div>

Hello,

Is here an existing / simple way to get at the lines of code per module and if conditionals are taking into account.

thanks,

Dan

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 17, 2021, 11:02am UTC](https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726/3 "2021-03-17T11:02:13Z")

</div>

thanks

Is there a way to pipe this into something that can count lines.

Ideally, i would write a little program that given a list of modules will generate a total line count

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 17, 2021, 11:09am UTC](https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726/4 "2021-03-17T11:09:13Z")

</div>

Looks like there is a way by redirecting standard output

> <https://stackoverflow.com/questions/32543457/redirecting-standard-output-stream>

But, i guess if this can be done in a streaming way – i.e. line by line.

Alternatively, i will just have to work with files and process them for example, one at a time.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 17, 2021, 11:49am UTC](https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726/5 "2021-03-17T11:49:54Z")

</div>

This seems like an interesting approach, using the pio library.

Need to think how this can be turned into a line-by-line count

> <https://stackoverflow.com/questions/11106921/read-entire-text-file-into-a-prolog-variable>

And the ultimate one for reading lines

> <https://stackoverflow.com/questions/31464096/swi-prolog-predicate-for-reading-in-lines-from-input-file>

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 17, 2021, 12:38pm UTC](https://swi-prolog.discourse.group/t/lines-of-code-in-modules-without-comments-and-with-conditional-compile-applied/3726/6 "2021-03-17T12:38:43Z")

</div>

If you are not too bothered that the listing is going to be read to a string, you could do something like this:

```prolog
with_output_to(string(X), listing(lists:_)),
aggregate_all(count, sub_string(X, _, _, _, "\n"), Count)

```

Careful, the toplevel will print out the full output of listing/1 as a string. The sub\_string/5 as used here counts empty lines as well, maybe you don’t want this.
