Published on InterSystems Developer Community (https://community.intersystems.com)

Home > The simplest snippet to read from file in InterSystems IRIS

Article
Evgeny Shvarov · May 11, 2016 1m read

The simplest snippet to read from file in InterSystems IRIS

Hi!

I believe the simplest is (to work with csv delimited by ";"):


set file = ##class(%File).%New( "data.csv" )
    set sc = file.Open( "R" ) 
    if $$$ISERR(sc) quit    ; or do smth

    while 'file.AtEnd {
        set str=file.ReadLine() 
        for i=1:1:$length( str, ";" ) {
            set id=$piece( str, ";" ,i ) 
            write !, id  // or do smth
        }
    }
    do file.Close()

Possible options:

different variants of error handling with sc code.

Embrace while loop into try/catch block.

And what's yours?

#Code Snippet #CSV #ObjectScript #Caché #InterSystems IRIS

Source URL:https://community.intersystems.com/post/simplest-snippet-read-file-intersystems-iris