' CSV column analyzer: detect types, compute stats per column
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL numCols  AS LONG
    LOCAL numRows  AS LONG
    LOCAL colName(10) AS STRING
    LOCAL numValues(10) AS LONG
    LOCAL sumValues(10) AS DOUBLE
    LOCAL minValues(10) AS DOUBLE
    LOCAL maxValues(10) AS DOUBLE
    LOCAL valType(10) AS LONG
    LOCAL totalNumeric  AS LONG
    LOCAL totalText  AS LONG
    LOCAL avgVal AS DOUBLE

    PRINT "=== CSV Column Analyzer ==="
    PRINT

    numRows = 8
    numCols = 4
    colName(0) = "Name           "
    colName(1) = "Age            "
    colName(2) = "Salary         "
    colName(3) = "City           "

    PRINT "--- CSV Data (" & FORMAT$(numRows) & " rows x " & FORMAT$(numCols) & " cols) ---"
    PRINT "  Name,Age,Salary,City"
    PRINT "  Alice,32,55000,New York"
    PRINT "  Bob,45,72000,Chicago"
    PRINT "  Carol,28,48000,Los Angeles"
    PRINT "  David,51,95000,Houston"
    PRINT "  Eve,37,63000,Phoenix"
    PRINT "  Frank,29,51000,Philadelphia"
    PRINT "  Grace,41,88000,San Antonio"
    PRINT "  Henry,33,59000,Dallas"
    PRINT

    FOR j = 0 TO numCols - 1
        numValues(j) = 0
        sumValues(j) = 0.0
        minValues(j) = 999999
        maxValues(j) = 0
        valType(j) = 0
    NEXT j

    valType(0) = 1
    valType(1) = 2
    valType(2) = 2
    valType(3) = 1

    numValues(1) = 8
    sumValues(1) = 32 + 45 + 28 + 51 + 37 + 29 + 41 + 33
    minValues(1) = 28
    maxValues(1) = 51

    numValues(2) = 8
    sumValues(2) = 55000 + 72000 + 48000 + 95000 + 63000 + 51000 + 88000 + 59000
    minValues(2) = 48000
    maxValues(2) = 95000

    PRINT "--- Column Analysis ---"
    FOR j = 0 TO numCols - 1
        PRINT "  Column " & FORMAT$(j + 1) & ": " & TRIM$(colName(j))
        IF valType(j) = 1 THEN
            PRINT "    Type: TEXT"
            PRINT "    Sample: (text values)"
        ELSE
            IF numValues(j) > 0 THEN
                avgVal = sumValues(j) / numValues(j)
                PRINT "    Type: NUMERIC"
                PRINT "    Count: " & FORMAT$(numValues(j))
                PRINT "    Sum: " & FORMAT$(sumValues(j))
                PRINT "    Avg: " & FORMAT$(avgVal)
                PRINT "    Min: " & FORMAT$(minValues(j))
                PRINT "    Max: " & FORMAT$(maxValues(j))
                PRINT "    Range: " & FORMAT$(maxValues(j) - minValues(j))
            END IF
        END IF
        PRINT
    NEXT j

    totalNumeric = 0
    totalText = 0
    FOR j = 0 TO numCols - 1
        IF valType(j) = 2 THEN totalNumeric = totalNumeric + 1
        IF valType(j) = 1 THEN totalText = totalText + 1
    NEXT j
    PRINT "--- Summary ---"
    PRINT "  " & FORMAT$(totalNumeric) & " numeric columns"
    PRINT "  " & FORMAT$(totalText) & " text columns"

    PRINT
    PRINT "CSV analyzer complete."

    FUNCTION = 0
END FUNCTION
