Q(uick)BASIC Statement: LSET
Quick View
LSET
A file I/O statement that moves data from memory to a random-access file buffer (in preparation for a PUT statement), left-justifies the value of a string in a string variable, or copies one record variable to another
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- LSET stringvariable$=stringexpression$
- RSET stringvariable$=stringexpression$
- LSET recordvariable1=recordvariable2
Description/Parameter(s)
stringvariable$ | Any string variable or a random-access file field defined in a FIELD statement. |
stringexpression$ | For LSET, the left-justified version of stringvariable$. For RSET, the right-justified version of stringvariable$. |
recordvariable1 | Record variables of any user-defined data type. |
recordvariable2 | Use LSET to assign a record variable of one data type to a different user-defined data type. |
Example
OPEN "FILEDAT.DAT" FOR RANDOM AS #1 LEN = 10
FIELD #1, 5 AS Ls1$, 5 AS Rs1$
LSET Ls1$ = "LSET"
RSET Rs1$ = "RSET"
PUT #1, 1
CLOSE #1
OPEN "FILEDAT.DAT" FOR RANDOM AS #1 LEN = 10
FIELD #1, 5 AS Ls2$, 5 AS Rs2$
GET #1, 1
PRINT "*" + Ls2$ + "*", "*" + Rs2$ + "*"
CLOSE #1
See also:
Syntax
- LSET {stringvariable=stringexpression | stringexpression1=stringexpression2}
Description/Parameter(s)
The stringvariable is usually a random-access file field defined in a FIELD statement, although it can be any string variable. The stringexpression is the value assigned to the variable.
If stringexpression requires fewer bytes than were defined for stringvariable in the FIELD statement, the LSET function left-justifies the string in the field (RSET will right-justify the string). Spaces are used to pad the extra positions. If the string is too long for the field, both LSET and RSET truncate characters from the right. Numeric values must be converted to strings before they are justified with the LSET or RSET statements.
You may also use LSET or RSET with a string variable not defined in a FIELD statement to left-justify or right-justify a string in a given field. For example, the program lines
- A$=SPACE$(20)
- RSET A$=N$
will right-justify the string N$ in a 20-character field. This can be useful for formatting printed output.
You can use LSET to assign one record variable to another. The following example copies the contents of RecTwo to RecOne:
- TYPE TwoString
- StrFld AS STRING * 2
- END TYPE
- TYPE ThreeString
- StrFld AS STRING * 3
- END TYPE
- DIM RecOne AS TwoString, RecTwo AS ThreeString
- ⋮
- LSET RecOne = RecTwo
Notice that LSET is used to assign record variables of differing types. Record variables of the same type can be assigned using LET. Also, because RecOne is only two bytes long, only two bytes are copied from RecTwo. LSET copies only the number of bytes in the shorter of the two record variables.
Examples
The following statement would convert the single-precision numeric variable AMT to a 4-byte string and stores that string in A$,left-justified:
LSET A$ = MKS$(AMT)
In contrast, the following statement would convert the integer numeric variable COUNT% to a 2-byte string and stores that string in D$, right-justified:
RSET D$ = MKI$(COUNT%)
Note: To see the LSET statement used in a full example program, see the ⮜ CVS function programming example ⮞
Syntax
- LSET stringvariable = stringexpression
- LSET recordvariable1 = recordvariable2
Description/Parameter(s)
stringvariable | A random-access file field defined in a FIELD statement, or any string variable. |
stringexpression | The value that is assigned to stringvariable and is left-justified. |
recordvariable1 | A record variable of any data type. |
recordvariable2 | A record variable of any data type. |
- LSET stringvariable = stringexpression
- The argument stringvariable usually is a random-access file field defined in a FIELD statement, although it can be any string variable.
- LSET recordvariable1 = recordvariable2
- You can use LSET to assign one record variable to another.
Usage Notes
- If stringexpression requires fewer bytes than were defined for stringvariable in the FIELD statement, the LSET function left-justifies the string in the field (RSET right-justifies the string). Spaces are used to pad the extra positions.
- If the string is too long for the field, both LSET and RSET truncate characters from the right.
- Numeric values must be converted to strings before they are justified with the LSET or RSET statement.
- LSET can be used with a string variable not defined in a FIELD statement to left- or right-justify a string in a given field. The following example right-justifies the string N$ in a 20-character field:
- A$ = SPACE$(20)
- LSET A$ = N$
- You can use LSET to assign one record variable to another. The following example copies the contents of RecTwo to RecOne. Only the number of bytes in the shorter of the two record variables are copied.
- TYPE TwoString
- StrFld AS STRING * 2
- END TYPE
- TYPE ThreeString
- StrFld AS STRING * 3
- END TYPE
- DIM RecOne AS TwoString, RecTwo AS ThreeString
- ⋮
- LSET RecOne = RecTwo
- Notice that this example demonstrates copying record variables of different data types. You also can use LSET to copy record variables of the same type.
Example
This example uses the LSET and RSET statements to assign values to fixed- and variable-length strings.
DIM TmpStr2 AS STRING * 25
CLS 'Clear screen.
'Use RSET on variable-length string with a value.
TmpStr$ = SPACE$(40)
PRINT "Here are two strings that have been right and left "
PRINT "justified in a 40-character variable-length string."
PRINT
RSET TmpStr$ = "Right-|"
PRINT TmpStr$
LSET TmpStr$ = "|-Left"
PRINT TmpStr$
PRINT "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
PRINT "12345678901234567890123456789012345678901234567890"
PRINT " 1 2 3 4 5"
'Use RSET on fixed-length string of length 25.
PRINT
PRINT
PRINT "Here are two strings that have been right and left"
PRINT "justified in a 25-character fixed-length string."
PRINT
RSET TmpStr2 = "Right-|"
PRINT TmpStr2
LSET TmpStr2$ = "|-Left"
PRINT TmpStr2$
PRINT "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
PRINT "12345678901234567890123456789012345678901234567890"
PRINT " 1 2 3 4 5"