Hi, This hopefully is a basic question. How do I equate a real to a
character if I know that the character array contains a number.
E.G.
real*8 X
character a(*)
x= real(a(0:10)) doesn't work but what does?
Thanks, Rudy
|
|
0
|
|
|
|
Reply
|
rjmagyar (14)
|
6/13/2006 4:13:10 PM |
|
Hello,
rjmagyar wrote:
> Hi, This hopefully is a basic question. How do I equate a real to a
> character if I know that the character array contains a number.
>
> E.G.
>
> real*8 X
> character a(*)
>
> x= real(a(0:10)) doesn't work but what does?
read( unit= a, fmt='...') x
Investigate "internal read" in your compiler's documentation
or a Fortran textbook.
Note that the length of the character variable ( the "*"
in the a(*) above) should be consistent with the format used
in the internal read.
HTH
--
Cheers!
Dan Nagle
Purple Sage Computing Solutions, Inc.
|
|
0
|
|
|
|
Reply
|
dannagle (1019)
|
6/13/2006 4:31:02 PM
|
|
rjmagyar <rjmagyar@gmail.com> wrote:
> Hi, This hopefully is a basic question. How do I equate a real to a
> character if I know that the character array contains a number.
>
> E.G.
>
> real*8 X
> character a(*)
>
> x= real(a(0:10)) doesn't work but what does?
Three things.
1. Just a terminology matter. What you want is not to "equate" them. If
you use that terminology, you will end up going down the wrong path.
What you want is to convert a value from one form to the other.
2. Do you really want to use character arrays instead of character
strings? Both are allowed, but character strings are more natural for
most Fortran purposes, including this one. Yes, there is a difference,
as comes up below.
3. With character strings, this is a FAQ, and the answer is an internal
READ. If "a" were a string instead of an array, you could do something
like
read(a,*) x
(Note. The use of list-directed formatting - the * here - is standard in
f90 and later, but not in f77, although most later f77 compilers accept
it as an extension. For string f77, you'd need an explicit format, which
can add extra complications if the string length is arbitrary).
For the character array case, the first thing you need to do is copy the
array into a temporary string.... and there are extra complications
there because the declaration form you used means that the size of the
array is unknown. I see that you used a(1:10) in your example. Does this
mean that you are guaranteed that both
a) The array is at least 10 elements in size
and
b) The data of interest (and nothing else) is all within the first 10
elements?
If both of these are true, then you could declare the temporary string
as
character*10 c
and do the copy with (using f90 DO loop syntax)
do i = 1 , 10
c(i:i) = a(i)
end do
Then you can do the internal read with
read(c,*) x
If either of the conditions is false, then that adds a little extra,
depending on exactly what you can guarantee.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
|
|
0
|
|
|
|
Reply
|
nospam47 (9742)
|
6/13/2006 4:34:16 PM
|
|
Dan Nagle <dannagle@verizon.net> wrote:
> Note that the length of the character variable ( the "*"
> in the a(*) above) should be consistent with the format used
> in the internal read.
Note that, unfortunately, the length of the character variable in
question is 1. The * is for the size of the character array. See my
other post. The form for a scalar character string would be a*(*)
instead of a(*) (or any of several alternative forms). Look ssimilar,
but the difference is important.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
|
|
0
|
|
|
|
Reply
|
nospam47 (9742)
|
6/13/2006 4:38:34 PM
|
|
Hello,
Richard E Maine wrote:
> Dan Nagle <dannagle@verizon.net> wrote:
>
>> Note that the length of the character variable ( the "*"
>> in the a(*) above) should be consistent with the format used
>> in the internal read.
>
> Note that, unfortunately, the length of the character variable in
> question is 1. The * is for the size of the character array. See my
> other post. The form for a scalar character string would be a*(*)
> instead of a(*) (or any of several alternative forms). Look ssimilar,
> but the difference is important.
You're right, sorry, I missed that.
But the lengths of the character variable and the length implied
by the format still must be consistent.
--
Cheers!
Dan Nagle
Purple Sage Computing Solutions, Inc.
|
|
0
|
|
|
|
Reply
|
dannagle (1019)
|
6/13/2006 4:53:21 PM
|
|
|
4 Replies
55 Views
(page loaded in 0.079 seconds)
|