Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: procedure to count number of words in a string #852

Open
nedtaylor opened this issue Jul 31, 2024 · 4 comments
Open

Proposal: procedure to count number of words in a string #852

nedtaylor opened this issue Jul 31, 2024 · 4 comments
Labels
idea Proposition of an idea and opening an issue to discuss it

Comments

@nedtaylor
Copy link

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

@nedtaylor nedtaylor added the idea Proposition of an idea and opening an issue to discuss it label Jul 31, 2024
@arjenmarkus
Copy link
Member

arjenmarkus commented Aug 1, 2024 via email

@nedtaylor
Copy link
Author

@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.

@jalvesz
Copy link
Contributor

jalvesz commented Aug 1, 2024

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

@jvdp1
Copy link
Member

jvdp1 commented Aug 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Proposition of an idea and opening an issue to discuss it
Projects
None yet
Development

No branches or pull requests

4 participants