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?
Ok, here is the "simplest"
(Well, I'm joking - this is the standard preamble for self-loading INT file)
This is MUMPS-Enigma code)
maybe enigmic,but i think it's very convient and good-short!
it's like my native tatarian tongue))))))
I would generally advise against using %File class for reading files as this is a very low level wrapper around the COS file commands. For example if you want to apply a translate table you need to know about the 'K' flag on the open command. I much prefer using the %Stream.FileBinary or %Stream.FileCharacter classes as these provide a much more consistent interface to the file.
Evgeny,
Thank you for sharing this snippet.
However, I don't understand why you de-piece a line with a delimiter of ";"?
I would just like to see the line like it is.
What am I missing here?
You have missed the extension of file "data.csv" used in the example. CSV stands for "Comma Separated Values", which is simplest way to exportt Excel like data to the textual file. And in this case it was apparently exported in the Russian-loale where "comma" is ";" actually.
Yes) Timur already answered. This snippet is not very general "read from file" snippet - but snippet to parse "russian-like" csvs) But every time when I work with text files line by line I use it.
Thank you for that clarification, but, should that be part of the opening documentation?
Didn't get it. You mean I should change the description for the snippet?
Or to change the snippet to make it less "csv" specific?
Just get rid of your inner for loop and you have a generic snippet that is helpful for everybody. The description doesn't speak of CSV files at all, though that may be an interesting use case as well. For CSV files, you can use the Record Mapper by the way:
http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...
Oh, thanks for this! I think it worth to make a separate snippet posting how to use the record mapper. Haven't found it in the documentation.
And since Yesterday here is new fashion way to make links to documentation ;)
Sorry, I am not criticizing what you have done.
I had to ask why you de-pieced the line based on ";"
and I was given an answer.
Suppose I did not ask, took your snippet and assumed to worked for all files?
All I am saying is the assumption you used (Russian style files) should be part of your documentation so someone not familiar with these style of files would know. Any (unfamiliar) assumptions should be documented.
Agreed. Will fix it. Anyway, you are very welcome to add your version of the simplest ever file management snippet ;)
Stefan,
Thanks for the tip on the Record Mapper, great stuff!