For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2006 > READ and delimited format









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author READ and delimited format
Jeremy

2006-12-27, 7:04 pm

Is it possible to use READ to read a delimited file? i.e.:

JOHN|DOE|123 MAIN ST.|SMALLTOWN|NJ|USA
JOE|SMITH|1 S. MAIN ST. APT 25|BIGTOWN|CA|USA

Jeremy
Beliavsky

2006-12-27, 7:04 pm


Jeremy wrote:
> Is it possible to use READ to read a delimited file? i.e.:
>
> JOHN|DOE|123 MAIN ST.|SMALLTOWN|NJ|USA
> JOE|SMITH|1 S. MAIN ST. APT 25|BIGTOWN|CA|USA


With a little work, yes. If you want to store the characters between
"|" in separate strings, you can

(1) Read each line into a large string with an "internal read"

read (iunit,"(a)") text

(2) Loop through "text" to determine the positions of the the
delimiters.

(3) Use character substring operations to store the appropriate parts
of "text" in separate character variables. For example

cc(1) = text(1:10) ; cc(2) = text(12:15)

if delimiters appear in the 11th and 16th positions of "text".

dpb

2006-12-27, 7:04 pm


Beliavsky wrote:
> Jeremy wrote:
>
> With a little work, yes. If you want to store the characters between
> "|" in separate strings, you can
>
> (1) Read each line into a large string with an "internal read"
>
> read (iunit,"(a)") text
>
> (2) Loop through "text" to determine the positions of the the
> delimiters.

....

And INDEX() intrinsic may be of help here.

Dan Nagle

2006-12-27, 7:04 pm

Hello,

I'm simply trying to clarify terminology a bit,
(some of these terms are used in this thread and the next)
but here goes ...

An external read or write transfers between an external unit
and program variables. So

character( len= 80) :: my_line
integer :: read_unit = 42

read( unit= read_unit, fmt= '( a)') my_line

is an example of a normal read from an external file.

OTOH, an internal read or write is a transfer between
an internal unit (a character variable) and other program variables.
So

character( len= 80) :: my_line, my_internal_unit

read( unit= my_internal_unit, fmt= '( a)') my_line

is an example of an internal read from the internal unit
my_internal_unit. (The example of an internal read
is especially pointless, as an assignment from one character variable
to the other would do the same thing. Internal reads/writes
are more useful when the format indicates some conversion.)

HTH

In the OP's example below, the next step is to walk through
the read string, using index() to find the next '|', something
like this

character( len= *), parameter :: separator_ch = '|'
character( len= 80) :: my_line, buffer
character( len= ??), dimension( ??) :: other_variables ...

integer :: idx

buffer = my_line ! keep my_line intact
idx = 1 ! assume there's at least one to start

break_at_separators: do while( idx > 0)

idx = index( buffer, separator_ch)

other_variables( ...) = buffer ( 1: idx-1)

buffer = buffer( 1: idx+1)

enddo break_at_separators

where I'm letting the reader work the indexing of the other_variables
to suit.

Beliavsky wrote:
> Jeremy wrote:
>
> With a little work, yes. If you want to store the characters between
> "|" in separate strings, you can
>
> (1) Read each line into a large string with an "internal read"
>
> read (iunit,"(a)") text
>
> (2) Loop through "text" to determine the positions of the the
> delimiters.
>
> (3) Use character substring operations to store the appropriate parts
> of "text" in separate character variables. For example
>
> cc(1) = text(1:10) ; cc(2) = text(12:15)
>
> if delimiters appear in the 11th and 16th positions of "text".
>



--
Cheers!

Dan Nagle
Purple Sage Computing Solutions, Inc.
Dan Nagle

2006-12-27, 7:04 pm

Hello,

Dan Nagle wrote:

<snip>

> break_at_separators: do while( idx > 0)
>
> idx = index( buffer, separator_ch)
>
> other_variables( ...) = buffer ( 1: idx-1)
>
> buffer = buffer( 1: idx+1)


Whoops! Should read buffer = buffer( idx+1: )

Note to self: Never write code before finishing coffee.

> enddo break_at_separators


<snip>

--
Cheers!

Dan Nagle
Purple Sage Computing Solutions, Inc.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com