• Welcome to OS2World OLD-STATIC-BACKUP Forum.
 

News:

This is an old OS2World backup forum for reference only. IT IS READ ONLY!!!

If you need help with OS/2 - eComStation visit http://www.os2world.com/forum

Main Menu

Parse a CSV-file and create a stem.

Started by jep, 2010.02.28, 11:29:46

Previous topic - Next topic

jep

Marked as: Easy
Hi,

this time I've looked at the file that the crc32 script* created to illustrate what one can do with the data to build a stem (array of values)

The function build a stem (array of values) in the form
csv.first_column_name.1
csv.first_column_name.2
...
csv.first_column_name.n
csv.second_column_name.1
...
based on what it can find on the first row.

You can also alter the behavior to the more traditional stems with integer values
csv.1.1
csv.1.2
...
csv.1.n
csv.2.1
...
if you remove the comment around "integer_style = 1"

You have the opportunity to specify how many rows should be parsed just as if you'd import the file with OpenOffice.org Calc, Lotus Smartsuite 123 or similar.
Specify a number as the second argument to the function to create a stem as a preview of just a few rows.

The delimiter character specify how to split up each row, while the symbol for quotation mark specify how to strip characters that surround text.


*Other applications also create and can use csv-files, so you can use this for other purposes as well.

/* Example how to parse a csv-file */

delimiter = ',' /* Change to the C2D(...) function for some characters like tab */
quotation_mark = '"'
/* integer_style = 1 */
CALL ParseCSV ARG(1)
IF csv.0 > 0 THEN
DO
   total = csv.1 /* total contain the name or number of the first column, we reuse the variable and dont use another */
   total = VALUE( 'csv.'total'.0' ) /* We assume that every column contain equally many rows */
END
ELSE RETURN 0

DO i = 1 TO total
   DO j = 1 TO csv.0
       col_name = csv.j
       SAY csv.col_name.i
   END
END

RETURN 0
/* Remove the line "RETURN 0" above to get to the code below */

/* Code below is specific to the file produced with the crc32 script */
DO i = 1 TO total
   j = i + 1
   DO WHILE csv.crc32.i = csv.crc32.j
       IF csv.filesize.i = csv.filesize.j THEN
       DO
           IF csv.computer.i <> csv.computer.j & csv.path.i = csv.path.j THEN j = j + 1
           ELSE DO
               SAY 'Delete 'csv.drive.j||csv.path.j||csv.name.j||'? (y/N)'
               PARSE PULL answer
               IF LEFT( TRANSLATE( answer ), 1 ) = 'Y' THEN /* Anything else than Y followed by Enter will be interpreted as NO, just Enter = NO as well */
                   '@del "'||csv.drive.j||csv.path.j||csv.name.j||'"'
           END
       END
       j = j + 1
   END
   i = j
END

RETURN total

ParseCSV: PROCEDURE EXPOSE csv. delimiter quotation_mark integer_style
   f_name = ARG(1)
   csv.0 = 0
   i = 0
   CALL STREAM f_name, 'C', 'OPEN'
   DO WHILE LINES( f_name ) > 0
       IF i = ARG(2) THEN RETURN 0 /* Only parse a few rows specified by ARG(2) */
       input = LINEIN( f_name )
       j = 0
       DO WHILE LENGTH( input ) > 0
           PARSE VALUE input WITH i_val(delimiter)input
           j = j + 1
           IF csv.0 = 0 THEN /* Build "header", use the names on the first row specifying the column names */
           DO
               IF VALUE( integer_style ) <> 'INTEGER_STYLE' THEN
               DO
                  csv.j = j
                  c_val = csv.j
               END
               ELSE DO
                    csv.j = TRANSLATE( STRIP( STRIP( i_val,, quotation_mark ) ) )
                   c_val = csv.j
              END
           END
           ELSE /* build the rows, use the names found on the first rows */
           DO
               c_val = TRANSLATE( csv.j )
               CALL VALUE 'csv.'c_val'.'i, STRIP( i_val,, quotation_mark )
           END
           CALL VALUE 'csv.'c_val'.0', i
       END
       IF csv.0 = 0 THEN
           csv.0 = j
       i = i + 1
   END
   CALL STREAM f_name, 'C', 'CLOSE'
RETURN


Example how the crc32.csv-file look like (with the addition of filesize not posted here in the forum yet)
"COMPUTER","DRIVE","PATH","NAME","CRC32","MODIFIED","FILESIZE"
"COMPUTERNAME","C:","\PROGRAMS\DSSAVER\Modules\PrClock\Digi\","38.png","007FB5DF","2006-01-12 13:31:30","282"
"COMPUTERNAME","D:","\CRC32TEST\PROGRAMS\DSSAVER\Modules\PrClock\Digi\","38.png","007FB5DF","2006-01-12 13:31:30","282"