Silverfrost Forums

Welcome to our forums

Reading records - each has words separated by spaces

29 May 2017 9:10 #19666

Need to make an alphabetically sorted list of phrases. Each phrase has several words separated by spaces. Although I have allocated an array for the records specifying expected max record length, each 'read' only 'strips off' the first word of each record, ignoring the rest. I have looked at all sorts of 'qualifiers' for the 'open file' and 'read record' statements without finding any solution.

There is the obvious, but to me very 'unpalatable', solution of replacing all the intervening spaces in any input record, by some other character, like an 'underscore'. But that, seems a clumsy solution (it's so easy to just tap the space bar between the words in an input record)!

Has any one met with, and overcome, this 'truncation of a read at intervening spaces' problem???

29 May 2017 9:45 #19667

I was getting a bit desperate to overcome my problem - but had a mini-brainwave after posting my query, that provided the solution! As with most things the answer is (sometimes) simple. I just changed my 'read' statement to read from the input file -** to include a specific 'format' declaration**. Making a guess that there wouldn't be many words in each phrase of each input record - I went for 'overkill' and specified '10a' in the specified 'format', and that solved my problem, reading all of each record - as one would expect!

30 May 2017 10:25 #19671

I think that my answer to the problem would be to read with (say) A80 and then count back through the trailing blanks to the first non-blank character, which would give me the length of the phrase including words and contained blanks.

If I wanted the words, and how many there were, I'd run through the string looking for separators.

FTN95 does something similar when scanning through a command line: it's in the help file under 'command line parsing options'. Sadly, you can't pretend that your string is a pseudo command line and let FTN95 do the hard work. However, there are some character handling routines in FTN77 that might prove helpful: the manual for that is in the 'Documentation' section of this website and can be downloaded as a PDF.

If Paul was looking for a job in an idle moment, he might please someone by producing the matching routines for sorting out words from a string in the same was as the command line parsing works (in the command line stuff the individual words are referred to as 'tokens' and the separators are somewhat different potentially from just 'blanks'. Perhaps all punctuation counts - I haven't thought that through).

It looks like a simple job to meet your needs, but a much more involved one to deal with the general case.

Eddie

1 Jun 2017 4:35 #19680

This works for me.

Declare a character variable whose length is sufficiient to contain the largetst record in the file.

Declare a character array whose elements are long enough for each word, and enough elements for the longest line.

Something like this:

character*4096 input_line
character*32 words(128)

read(1,'(a)')input_line
read(input_line,*,end=100)(words(i),i=1,128)
100 i=i-1 ! i now contains the number of words in that line

Process away!

Please login to reply.