Lasso Soft Inc. > Home

[ file ]


[file] creates an object that Lasso can use to interact with a file in the filesystem. You can create an empty file object and specify the path later, or you can call the creator method with the path to a file as a parameter. You can also optionally specify  the open and get mode indicators.

When a file connection is opened using the [file] tag, several different open modes can be used. These modes optimize the file connection for best performance depending on the purpose of the connection. A get mode may also be specified to determine how the file will be read (by line or by character). The open and get modes are described below.

File Get Modes
  • [file_modeline] passing this method in for the get mode sets the file object to be read one line at a time
  • [file_modechar] passing this method in for the get mode sets the file object to be read one character at a time
File Open Modes

It is possible to combine various modes by using [integer_bitOr] method. For example, if you wanted to open a file for creation and reading and writing, you could do this:

[var(file) = file('/path/to/file', integer_bitOr(io_file_o_creat, io_file_o_rdwr))]

The [file] type also comes with the following member tags cover the more common open modes: [file->openRead], [file->openWrite], [file->openWriteOnly], [file->openAppend], and [file->openTruncate]

  • Syntax
  • Methods
  • Traits
var(file) = file()

var(file) = file(path, open_mode, get_mode)
Has methods:
  • Beginner
  • Advanced

Get the contents of a file as a string object

The code below will read the entire contents of a file and return it as a string.


local(f) = file('/path/to/file.txt')
handle => { #f->close }


#f->readString returns the contents of the file as a string, so the result is just the contents of the file

Count the number of fields in a CSV file

The code below counts the number of fields in a CSV file. Since a CSV file could have a field broken across multiple lines, the code is setup to read a line at a time and check to see if it has a complete CSV row. If not, it continues reading lines until it does.

To set this up, the file object is created with this call: [file(#path, io_file_o_rdonly, file_modeLine)]. This opens the specified file specifying the open mode as read only and the get mode as returning a line at a time instead of a character at a time.


define csv_fieldCount(path::string)::integer => {
    // Open a file to read lines not characters
    local(data_file) = file(#path, io_file_o_rdonly, file_modeLine)
    handle => {#data_file->close}

    // Parsing the CSV file
    // Keep reading lines until we run out of lines or until we get a complete CSV line to count # of fields
    local(csv_line)     = ''
    local(parsed_csv)   = array
    local(need_joining) = ''
    while(true) => {
        #csv_line = string(#data_file->get)
        fail_if(not #csv_line, -1, "NOT A CSV FILE: " + #path)

        iterate(#csv_line->split(',')) => {
            if(loop_value->first != '"' && #need_joining == '') => {
            // Count the ending number of " that don't include \" 
            // (for cases such as \"""", the first " won't be included since it is preceded by a backslash)
            // If it ends in an even number, we don't have a properly closed off field yet
            local(count_quotes) = regexp(-input=loop_value, -find=`(?:[^\\])("+)$`)
            if(loop_value != '"' and (not #count_quotes->find or #count_quotes->matchString(1)->size % 2 == 0)) => {
                #need_joining += loop_value + ','
            #need_joining += loop_value->remove(loop_value->size)&
            #need_joining = ''

        if(#need_joining == '') => { loop_abort }
        #need_joining += "\n"

    return #parsed_csv->size

// Assume the contents of /path/to/file.csv look like this:
// "One, field",2,,4,"Fifth field",6



Recent Comments

No Comments found

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home



©LassoSoft Inc 2014 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft