fortran-lang/stdlib

Proposal: procedure to count number of words in a string

nedtaylor opened this issue · 4 comments

Motivation

I regularly need to count the number of delimiter-separated fields in a string.

When files have an arbitrary number of fields in a line, and you need to read them in to an allocatable array. Reading the line to a buffer and counting the number of fields in a line so that the array can first be allocated is very handy.

This is also particularly useful in the following situation:
line = "Hello there how are you?
where the delimiter is repeated, and you want to just count the number of delimiter-separated fields.

Prior Art

  • wordcount (wc) in bash

Additional Information

No response

@arjenmarkus. Quite possibly, depends on how you want to use it. I imagine that one would then use:

call split(line, array)
number = size(array)

However, split works by storing in character arrays (from what I can see), so it uses up unnecessary memory if wanting to read integers or reals, or just get the number of fields on a line.

I think there's still a use for it, but you may be completely right and I'm just overthinking this.

This is a good idea, you could slightly modify the procedure to be a function returning by default the number of elements found in the line. Also, the output array can be an optional argument to avoid the allocation if not needed, for instance: https://godbolt.org/z/bz461x9G6

I support this idea.

Note that stdlib already includes the functions number_of_columns and number_of_rows. These functions are currently private. But they could be actually public.