Fortran FORTRAN conversion from int to char OR zero suppression

  • Thread starter Thread starter sergegirard
  • Start date Start date
  • Tags Tags
    Fortran Zero
Click For Summary
The discussion revolves around a FORTRAN programming challenge where the user seeks to print an INTEGER array while suppressing the display of zeroes, replacing them with spaces. The user attempted to convert the INTEGER values to CHARs using the ACHAR and ICHAR functions but encountered unexpected characters in the output. A participant clarified that ACHAR and ICHAR are not suitable for converting integers to their string representations, as they deal with ASCII codes instead. Instead, they suggested using an internal file approach to convert the INTEGER array to a character array, allowing for conditional formatting. The provided example demonstrates how to create a character array from the INTEGER array and replace zeroes with spaces. The discussion also hints at a simpler solution where the user could directly check for numeric zeroes during printing, avoiding unnecessary conversions.
sergegirard
Messages
1
Reaction score
0
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
 
Technology news on Phys.org
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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
30K
  • · Replies 3 ·
Replies
3
Views
16K