FORTRAN conversion from int to char OR zero suppression

  • Context: Fortran 
  • Thread starter Thread starter sergegirard
  • Start date Start date
  • Tags Tags
    Fortran Zero
Click For Summary
SUMMARY

The discussion focuses on converting an INTEGER array to a CHAR array in FORTRAN while suppressing the printing of zeroes. The user, Serge, seeks to replace zeroes with spaces in the output. The solution involves using internal files for conversion, as the functions ICHAR and ACHAR do not perform the desired numeric-to-character transformation. A sample program demonstrates how to achieve this by writing integers to a character array and conditionally replacing zeroes with blanks.

PREREQUISITES
  • Understanding of FORTRAN programming language
  • Familiarity with INTEGER and CHARACTER data types in FORTRAN
  • Knowledge of internal file operations in FORTRAN
  • Basic understanding of ASCII values and their conversions
NEXT STEPS
  • Explore FORTRAN internal file operations for data conversion
  • Learn about character array manipulation in FORTRAN
  • Investigate the use of conditional statements in FORTRAN for output formatting
  • Study the differences between ICHAR and ACHAR functions in FORTRAN
USEFUL FOR

This discussion is beneficial for FORTRAN programmers, particularly those working on data formatting and output manipulation. It is also useful for developers transitioning from other programming languages who need to understand FORTRAN's unique data handling capabilities.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
6
Views
4K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K