iSeries / AS400 FAQ : Programming : RPG : How can I locate a fields value by the fields name?
You can’t do this in general in RPG (or any compiled language), but if the field is in an externally-described file, you can define an externally-described data structure for the file to get the fields in the same layout in the program as they are in the file. Then you can call the QUSLFLD API to find where that field is in the data structure. You can also use this technique to do generic processing on files. For example, if you want to check several files to ensure that all zoned fields have valid data, you could use the program below. It uses a program described file, and calls QUSLFLD to find out the location and length of all the zoned fields in the file. Then it reads the file and checks the zoned fields in every record. If you wanted to find a particular field or set of fields by name, rather than looking for type = ‘S’ as this program does, you’d look for the name you were interested in (subfield fieldName).
You can’t do this in general in RPG (or any compiled language), but if the field is in an externally-described file, you can define an externally-described data structure for the file to get the fields in the same layout in the program as they are in the file. Then you can call the QUSLFLD API to find where that field is in the data structure. You can also use this technique to do generic processing on files. For example, if you want to check several files to ensure that all zoned fields have valid data, you could use the program below. It uses a program described file, and calls QUSLFLD to find out the location and length of all the zoned fields in the file. Then it reads the file and checks the zoned fields in every record. If you wanted to find a particular field or set of fields by name, rather than looking for type = ‘S’ as this program does, you’d look for the name you were interested in (subfield fieldName).