oacs_util::csv_foreach (public)

 oacs_util::csv_foreach -file file [ -header_line header_line ] \
    [ -override_headers override_headers ] -array_name array_name \
    code_block

Defined in packages/acs-tcl/tcl/utilities-procs.tcl

Reads a CSV string and executes code block for each row in the CSV.

Switches:
-file
(required)
the CSV file to read.
-header_line
(defaults to "1") (optional)
the line with the list of var names
-override_headers
(optional)
the list of variables in the CSV
-array_name
(required)
the name of the array to set with the values from the CSV as each line is read.
Parameters:
code_block

Partial Call Graph (max 5 caller/called nodes):
%3 test_csv_foreach csv_foreach (test acs-tcl) oacs_util::csv_foreach oacs_util::csv_foreach test_csv_foreach->oacs_util::csv_foreach

Testcases:
csv_foreach
Source code:
    set csv_stream [open $file r]

    # Check if there are headers
    if {$override_headers ne ""} {
        set headers $override_headers
    } else {
        if {!$header_line} {
            return -code error "There is no header!"
        }

        # get the headers
        ns_getcsv $csv_stream headers
    }

    # provide access to errorCode

    # Upvar Magic!
    upvar 1 $array_name row_array

    while {1} {
        # Get a line
        set n_fields [ns_getcsv $csv_stream one_line]

        # end of things
        if {$n_fields == -1} {
            break
        }

        # Process the row
        for {set i 0} {$i < $n_fields} {incr i} {
            set varname [string tolower [lindex $headers $i]]
            set varvalue [lindex $one_line $i]
            set row_array($varname$varvalue
        }

        # Now we are ready to process the code block
        set errno [catch { uplevel 1 $code_block } error]

        if {$errno > 0} {
          close $csv_stream
        }

        # handle error, return, break, continue
        # (source: https://wiki.tcl-lang.org/unless last case)
        switch -exact -- $errno {
            0   {}
            1   {return -code error -errorinfo $::errorInfo  -errorcode $::errorCode $error}
            2   {return $error}
            3   {break}
            4   {}
            default     {return -code $errno $error}
        }
    }
XQL Not present:
PostgreSQL, Oracle
Generic XQL file:
packages/acs-tcl/tcl/utilities-procs.xql

[ hide source ] | [ make this the default ]
Show another procedure: