Empty string: Difference between revisions

→‎{{header|Fortran}}: Clarify the stages of furrytran text gnashing.
m (added whitespace before the TOC.)
(→‎{{header|Fortran}}: Clarify the stages of furrytran text gnashing.)
Line 570:
 
=={{header|Fortran}}==
Early Fortran doesoffered notonly offerrather strange methods of manipulating text, involving overwriting text literals within FORMAT statements via a READ statement that used that format statement. Fortran 4 introduced the A format whereby text could be stored in integer or floating-point variables or arrays, and then those variables could be manipulated. Fortran 77 introduced a CHARACTER definition which greatly eased matters but it was not a "string" type, which is to say, a variable storing some sequence of characters (or, in principle, integers, or other data) and ''also'' having a length. A variable may be declared as having a fixed size, as in CHARACTER*24 TEXT, and there is a library function LEN which for that variable would return 24, no matter what the variable contained. That is to say, it reports the size of the variable, not the length in current use of a string of up to 24 characters as would be the case for a similar declaration in for example, Pascal.
 
Such variables, or text literals, may be passed as a parameter to a subprogram, and it may use the LEN function to ascertain the size of the parameter, which in that sense could be considered a string because CHARACTER parameters are passed with a secret additional parameter, their size, which is available to the LEN function within the subprogram.
<lang fortran> SUBROUTINE TASTE(T)
SUBROUTINE TASTE(T)
CHARACTER*(*) T !This form allows for any size.
IF (LEN(T).LE.0) WRITE(6,*) "Empty!"
Line 584 ⟶ 583:
TEXT = "" !Fills the entire variable with space characters.
CALL TASTE(TEXT) !Passes all 24 of them. Result is Not empty!
END</lang>
</lang>
 
Otherwise, you could employ the Fortran protocol that trailing spaces are irrelevant in text comparisons. Thus <code>TEXT .EQ. ""</code> would give ''true'' even though TEXT might contain thousands of space characters, and so would <code>TEXT .EQ. " "</code> - thus an empty string is one containing nothing other than spaces.
 
Alternatively, the programmer can be diligent, and associate an integer with every such CHARACTER variable, such as LTEXT for TEXT, to hold the current length of characters in use. Tests for empty strings and the like would thus be made by inspecting the value of LTEXT, which hopefully, would always contain correct values. With later versions of Fortran, compound data aggregates can be defined and as well procedures for operating on them, so that, after a great deal of syntactic struggle, a string data type will be available.
 
With F90, compound data aggregates can be defined and as well procedures for operating on them, so that, after a great deal of syntactic struggle, a string data type will be available. F2000 standardised one such scheme whereby character variables are de-allocated and re-allocated with usage so that a statement such as <code>TEXT = "This" // "That"</code> would cause a de-allocation of whatever storage had been associated with TEXT followed by a re-allocation of storage for eight characters, the required size, and LEN(TEXT) would give 8.
 
=={{header|Go}}==
Go has no special syntax for empty strings.
1,220

edits