FORTRAN conversion from int to char OR zero suppression

• Fortran
Hello,

I am new with FORTRAN but experienced with PLI/I, Cobol (mainframe), Clipper (PC) etc.

As an exercise I'm writing some small pograms.

Now I have one problem/question:

I have an INTEGER array (ARPAS). When printing this array I want to suppress printing the zeroes; only # 0. This option I can't find.

Then I tried to make a turn-a-round: convert those INTEGERs to CHARs (CHARPAS):

CHARPAS (RIJ , KOL) = ACHAR(ARPAS (RIJ , KOL) ) and even
CHARPAS (RIJ , KOL) = ICHAR(ARPAS (RIJ , KOL) )

and then conditioned printed (IF char .EQ. '0' print a space..)

Result is a lots of ☺ etc.

Input is : 0 1 0 7 0 21 0 35 0 35 0 21 0 7 0 1 0
and I want is : bb 1 bb 7 bb 21 bb 35 bb 35 bb 21 bb 7 bb 1 bb

where bb is blanks/spaces.

Any idea what goed wrong?

Thanks,
Serge/Belgium

alphysicist
Homework Helper
Hi sergegirard,

I don't think I really understand exactly how you want your output formatted, so I may not be giving you the right information. But if you want to convert from a numeric to a character array here is one way.

The functions ichar and achar do not convert values from integer to character and vice versa, at least not in the sense of of taking the number 35 and converting it to the string '35'. They convert between a symbol and its ASCII code, so for my system I get:

print*,ichar('a') -----> prints out the number 97
print*,achar(97) -----> prints out the letter 'a'

because 97 is the ASCII code for 'a'.

To convert from integer to character you can use an internal file. I threw together a quick program to show you how the internal files might work; it works for the particular case you wrote in your posting, so maybe you can get some ideas for your particular problem. (It would need heavy editing to be a general purpose program.) It prints out your integer array, the character array with zeros, and the character array with zeros converted to an empty string (and therefore filled with blanks).

Code:
program internal_files
implicit none

integer, dimension(17)::arpas
character(len=3),dimension(17)::charpas
integer::i

arpas = (/ 0,1,0,7,0,21,0,35,0,35,0,21,0,7,0,1,0 /)

print*,arpas

do i=1,size(arpas)
write(charpas(i),*) arpas(i)          !internal file used here
enddo

print*,charpas                           !character array with zeros

where (charpas.eq.' 0')
charpas = ''
end where

print*,charpas                           !character array without zeros

end program internal_files

The dimension of the character array elements must be at least 1 greater than the number of digits of your integers (to handle the space for the sign).

Anyways, like I said I was not clear as to how you wanted to output the data since you did not include any details of the program in your posting, but maybe the above will give you some ideas.

I guess I don't understand ...

If I understand you correctly, your willing to expend the cost of converting the numeric to character, testing the character for '0' and then conditionally print with two different formats depending on the outcome of the the test.

If that's the case, then why not simply check for numeric zero and then proceed with your printing options.