For Programmers: Free Programming Magazines  


Home > Archive > Fortran > March 2004 > Problem with Read while reading part of character arrays.









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 Problem with Read while reading part of character arrays.
Noby

2004-03-27, 12:17 am

I wanted to read 2 characters from the ARRAY and store it in VAL as integer.
But the above program gives only one character as output.

Is it possible to apply formatted IO operations on Character arrays.
(I know that it is possible on character strings)

PROGRAM MAIN

INTEGER VAL
CHARACTER ARRAY(5)

DATA ARRAY(1) /'1'/
DATA ARRAY(2) /'2'/
DATA ARRAY(3) /'3'/
DATA ARRAY(4) /'4'/
DATA ARRAY(5) /'5'/

READ(ARRAY(2),100)VAL
100 FORMAT(I2);

WRITE(6,*)VAL
END

Output of the above program:
2

I have expected 23
meek@skyway.usask.ca

2004-03-27, 12:17 am

In a previous article, nobyjos@indiatimes.com (Noby) wrote:
>I wanted to read 2 characters from the ARRAY and store it in VAL as integer.
>But the above program gives only one character as output.
>
>Is it possible to apply formatted IO operations on Character arrays.
>(I know that it is possible on character strings)
>
> PROGRAM MAIN
>
> INTEGER VAL
> CHARACTER ARRAY(5)
>
> DATA ARRAY(1) /'1'/
> DATA ARRAY(2) /'2'/
> DATA ARRAY(3) /'3'/
> DATA ARRAY(4) /'4'/
> DATA ARRAY(5) /'5'/
>
> READ(ARRAY(2),100)VAL
>100 FORMAT(I2);
>
> WRITE(6,*)VAL
> END
>
>Output of the above program:
> 2
>
>I have expected 23

Try

Character*5 array
data array/'12345'/
read(array,'(1x,i2)')val

for example.

There are many ways to do what you're trying to do, depending on
what that is. What is the (programming) problem you are trying to
solve ?

Chris
Jan C. Vorbrüggen

2004-03-27, 12:17 am

The natural way to handle text data in Fortran is a string, i.e., a
CHARACTER variable of some useful length. Although a CHARACTER array
looks superficially similar, it's not the same thing - in particular,
_each_ array element is a string by itself! So, either use the approach
shown in the first reply, or TRANSFER the array to a character string
before reading the value(s). See the recent lecture by James van Buskirk
on how to use TRANSFER in this (and other) situation(s).

Jan
Michael Metcalf

2004-03-27, 12:17 am


"Noby" <nobyjos@indiatimes.com> wrote in message
news:1528aa0c.0403230522.78986c14@posting.google.com...
> Is it possible to apply formatted IO operations on Character arrays.
> (I know that it is possible on character strings)
>


Yes, but you have to be aware that each element is treated as a separate
record ("Fortran 90/95 Explained", Ch. 9). Thus, to do what you want
requires a handstand:

READ(ARRAY(2:3),100)VAL1, val2
100 FORMAT(I1);
WRITE(6,*)VAL1*10 + val2

Regards,

Mike Metcalf



glen herrmannsfeldt

2004-03-27, 12:17 am

Noby wrote:

> I wanted to read 2 characters from the ARRAY and store it in VAL as integer.
> But the above program gives only one character as output.


> Is it possible to apply formatted IO operations on Character arrays.
> (I know that it is possible on character strings)


> PROGRAM MAIN
>
> INTEGER VAL
> CHARACTER ARRAY(5)
>
> DATA ARRAY(1) /'1'/
> DATA ARRAY(2) /'2'/
> DATA ARRAY(3) /'3'/
> DATA ARRAY(4) /'4'/
> DATA ARRAY(5) /'5'/


> READ(ARRAY(2),100)VAL
> 100 FORMAT(I2);
>
> WRITE(6,*)VAL
> END


> Output of the above program:
> 2


> I have expected 23


You gave it an array element, and it gave you the value in
that array element.

If you give it the whole array, it will consider each
element as a record. You could read it with

100 FORMAT(I1/I1/I1/I1/I1)

While passing an array element to a subroutine implies the
rest of the array starting at that element (does it still do that?),
that is not true for READ or WRITE statements, either in this
case or in the I/O list.

-- glen

Sponsored Links







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

Copyright 2009 codecomments.com