For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2007 > fortran // concat /> fortranconcat









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 fortran // concat /> fortranconcat
aeroguy

2007-07-18, 4:16 am

program chartest
integer :: i
character(len=10) :: ast
character(len=5) :: num
i=30
write(num,*)i
ast='SS'//trim(num)

write(*,*) i,ast
pause
end program chartest

I need the output as SS30 but i am getting SS 30 and if i reduce the
len of num to 2 i am getiing "writing more data than the record
size" error.

Can anyone help how to get this thing done?

thnaks and regards

Richard Maine

2007-07-18, 4:16 am

aeroguy <sukhbinder.singh@gmail.com> wrote:

> program chartest
> integer :: i
> character(len=10) :: ast
> character(len=5) :: num
> i=30
> write(num,*)i
> ast='SS'//trim(num)

....
> I need the output as SS30 but i am getting SS 30 and if i reduce the
> len of num to 2 i am getiing "writing more data than the record
> size" error.


List-directed output always starts each record with a blank. The reasons
for this are historical and largely irrelevant. Even though this is an
internal write, the same rules still apply, including the business about
starting with a blank.

The trim intrinsic only trims trailing blanks - not leading ones. The
simplest fix is to use trim(adjustl(num)). The adjustl() left justifies
the data, essentially moving any leading blanks to the end, where they
are then removed by the trim(). There are other ways to do it, but
that's an easy one.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
David Jones

2007-07-18, 4:16 am

Richard Maine wrote:
> aeroguy <sukhbinder.singh@gmail.com> wrote:
>=20
> ...
>=20
> List-directed output always starts each record with a blank. The
> reasons for this are historical and largely irrelevant. Even though
> this is an internal write, the same rules still apply, including the
> business about starting with a blank.
>=20
> The trim intrinsic only trims trailing blanks - not leading ones. The
> simplest fix is to use trim(adjustl(num)). The adjustl() left
> justifies the data, essentially moving any leading blanks to the end,
> where they are then removed by the trim(). There are other ways to do
> it, but that's an easy one.


In this instance, the "trim" step can be omitted as the result is =
extended by blanks at the assignment stage. This just the following =
would do:

ast=3D'SS'//adjustl(num)

with a possible use of trim(ast) at a later step if necessary.

David Jones

Jan Vorbrüggen

2007-07-18, 8:06 am

>>program chartest
>
> ....
>
>
> List-directed output always starts each record with a blank. The reasons
> for this are historical and largely irrelevant. Even though this is an
> internal write, the same rules still apply, including the business about
> starting with a blank.


I would say the correct 8-) answer is, "don't use list-directed output". In
this case, a format of I2.2 seems appropriate. Or perhaps I5.5. Or any other
variant, depending on details of your requirements.

Jan
John Paine

2007-07-19, 4:19 am


"aeroguy" <sukhbinder.singh@gmail.com> wrote in message
news:1184744018.417311.189910@j4g2000prf.googlegroups.com...
> program chartest
> integer :: i
> character(len=10) :: ast
> character(len=5) :: num
> i=30
> write(num,*)i
> ast='SS'//trim(num)
>
> write(*,*) i,ast
> pause
> end program chartest
>
> I need the output as SS30 but i am getting SS 30 and if i reduce the
> len of num to 2 i am getiing "writing more data than the record
> size" error.
>
> Can anyone help how to get this thing done?
>
> thnaks and regards
>


The problem is that the free format is adding leading blanks and the
intrinsic function TRIM() only removes trailing blanks.

In CVF there is an intrinsic function ADJUSTL(string) which shifts the
string left to remove leading blanks. So the code:

ast='SS'//trim(adjustl(num))

should give the result you want.

If you don't have CVF then check if there is some function equivalent to the
CVF one (eg TRIML()) or use a fixed integer format such as I2.2 or write
your own code to remove the leading blanks from num.





Richard Maine

2007-07-19, 4:19 am

John Paine <johnpaine1@optusnet.com.au> wrote:

> In CVF there is an intrinsic function ADJUSTL(string) which shifts the
> string left to remove leading blanks. So the code:


That's a standard f90 intrinsic. It will be in any f90 compiler. And
since the OP was already using trim(), which is also an f90 intrinsic,
that makes it pretty likely that he is using an f90/f95 compiler.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
John Harper

2007-07-22, 7:10 pm

In article <1i1f8jh.z7151bun5pumN%nospam@see.signature>,
Richard Maine <nospam@see.signature> wrote:[color=darkred]
>aeroguy <sukhbinder.singh@gmail.com> wrote:
>
>...

Richard's solution works, and so does

write(num,'(I0)')i
ast='SS'//num

because 'SS'//trim(num) would then be padded with 6 blanks on the right,
and 'SS'//num has 3 blanks there to start with and is padded with 3
more. The result is that ast is 'SS30 ' with 6 blanks after the 0 .

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
Sponsored Links







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

Copyright 2008 codecomments.com