Help with simple parsing in F95
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.fortran Profile…
 Up
Help with simple parsing in F95         


Author: deltaquattro
Date: Jun 4, 2008 09:55

Hi,

I have to scan through a text file and find the line number of the
first line, if any, where a number N occurs as the first sequence of
characters after any leading spaces. Example: foo.txt, N=23

SSD
544
fdskfdsjh 23
d fsfdskjh
34 23

23
....

In this case the required output is 7, since it's the number of the
first line where 23 occurs as the first sequence of characters after
the leading spaces. I know I have to read each line as a string until
I find the EOF character, but then how do I perform the scan inside
each string in order to find if N appears as the first sequence of
characters after leading spaces? Thanks,

best regards,

deltaquattro
25 Comments
Re: Help with simple parsing in F95         


Author: fj
Date: Jun 4, 2008 11:06

On 4 juin, 18:55, deltaquattro gmail.com> wrote:
> Hi,
>
> I have to scan through a text file and find the line number of the
> first line, if any, where a number N occurs as the first sequence of
> characters after any leading spaces. Example: foo.txt, N=23
>
> SSD
> 544
> fdskfdsjh 23
> d fsfdskjh
> 34 23
>
> 23
> ....
>
> In this case the required output is 7, since it's the number of the
> first line where 23 occurs as the first sequence of characters after
> the leading spaces. I know I have to read each line as a string until
> I find the EOF character, but then how do I perform the scan inside ...
Show full article (0.95Kb)
no comments
Re: Help with simple parsing in F95         


Author: James Giles
Date: Jun 4, 2008 11:29

fj wrote:
> On 4 juin, 18:55, deltaquattro gmail.com> wrote:
...
>> SSD
>> 544
>> fdskfdsjh 23
>> d fsfdskjh
>> 34 23
>>
>> 23
>> ....
>>
>> In this case the required output is 7, [...]
...
> PROGRAM findline
>
> INTEGER :: i,k,n
> i=0
> DO
> i=i+1 ...
Show full article (0.99Kb)
no comments
Re: Help with simple parsing in F95         


Author: Paul van Delst
Date: Jun 4, 2008 11:32

fj wrote:
> On 4 juin, 18:55, deltaquattro gmail.com> wrote:
>> Hi,
>>
>> I have to scan through a text file and find the line number of the
>> first line, if any, where a number N occurs as the first sequence of
>> characters after any leading spaces. Example: foo.txt, N=23
>>
>> SSD
>> 544
>> fdskfdsjh 23
>> d fsfdskjh
>> 34 23
>>
>> 23
>> ....
>>
>> In this case the required output is 7, since it's the number of the
>> first line where 23 occurs as the first sequence of characters after
>> the leading spaces. I know I have to read each line as a string until ...
Show full article (2.09Kb)
no comments
Re: Help with simple parsing in F95         


Author: James Giles
Date: Jun 4, 2008 11:50

Paul van Delst wrote:
...
> Of course, there is the size of the "buffer" variable to consider.

You could use non-advancing I/O using a buffer that's only
one character long. If you reach end of line you increment
the line counter and start again. If you find a non-blank character
that's not a 2, skip the rest of the line (use advancing I/O),
increment the line counter and start again. If you find a 2 but
the next character isn't 3, you skip the rest of the line etc. If
the character after the first 2 *is* a 3, you write the current
line number and stop.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
no comments
Re: Help with simple parsing in F95         


Author: e p chandler
Date: Jun 4, 2008 13:44

On Jun 4, 12:55 pm, deltaquattro gmail.com> wrote:
> Hi,
>
> I have to scan through a text  file and find the line number of the
> first line, if any, where a number N occurs as the first sequence of
> characters after any leading spaces. Example: foo.txt, N=23
>
> SSD
>  544
> fdskfdsjh 23
>   d fsfdskjh
>   34 23
>
>       23
> ....
>
> In this case the required output is 7, since it's the number of the
> first line where 23 occurs as the first sequence of characters after
> the leading spaces. I know I have to read each line as a string until
> I find the EOF character, but then how do I perform the scan inside ...
Show full article (1.08Kb)
no comments
Re: Help with simple parsing in F95         


Author: deltaquattro
Date: Jun 5, 2008 02:53

On 4 Giu, 20:06, fj wrote:
> On 4 juin, 18:55,deltaquattrogmail.com> wrote:
>
>
>
>
>
>> Hi,
>
>> I have to scan through a text  file and find the line number of the
>> first line, if any, where a number N occurs as the first sequence of
>> characters after any leading spaces. Example: foo.txt, N=23
>
>> SSD
>>  544
>> fdskfdsjh 23
>>   d fsfdskjh
>>   34 23
>
>>       23 ...
Show full article (1.34Kb)
no comments
Re: Help with simple parsing in F95         


Author: deltaquattro
Date: Jun 5, 2008 03:11

On 4 Giu, 20:32, Paul van Delst noaa.gov> wrote:
> fj wrote:
[..]
>
> PROGRAM findline
>    implicit none
>    character(100) :: buffer
>    character(2) :: pattern='23'
>    integer :: i, n
>    i=0
>    do
>      i=i+1
>      read(*,'(a)') buffer
>      buffer = adjustl(buffer)
>      if ( index(buffer,pattern) == 1 ) exit
>    end do
>    write(*,'(2x,"Pattern found on line ",i0)') i
> END PROGRAM findline
>
> ?? ...
Show full article (2.20Kb)
no comments
Re: Help with simple parsing in F95         


Author: deltaquattro
Date: Jun 5, 2008 03:18

On 5 Giu, 12:11, deltaquattro gmail.com> wrote:
> On 4 Giu, 20:32, Paul van Delst noaa.gov> wrote:
[..]
>     call scan(trim(adjusl(pattern),line_number,error)

Whoops! Obviously that would be

call scan(buffer,trim(adjusl(pattern),line_number,error)

and

SUBROUTINE scan(buffer,pattern,line_number,error)
character(*), intent(IN) :: buffer
    character(*), intent(IN)  :: pattern
    integer,      intent(OUT) :: line_number
    logical,      intent(OUT) :: error

    integer        :: i,k
Show full article (1.00Kb)
no comments
Re: Help with simple parsing in F95         


Author: deltaquattro
Date: Jun 5, 2008 03:29

On 4 Giu, 20:32, Paul van Delst noaa.gov> wrote:
[..]
>
> lnx:/scratch : a.out
> SSD
> 23fhgryd
>       Pattern found on line 2
>
> lnx:/scratch : a.out
> SSD
> 236754
>       Pattern found on line 2
>
Ouch! I didn't explain myself very well, sorry! Neither 23fhgryd or
236754 must match 23. 23fhgryd cannot occur in my file (it can contain
both numbers and strings, but always separed by spaces). 236754 can
occur, and since it's not the same number than 23 it must not match.
Any ideas?

Greetings,
Show full article (0.54Kb)
no comments
1 2 3