|
Imports records from a text file one line at a time where each line ends with a line feed (lf). The tag loops through the file stopping at the end of file (eof), unless terminated using
Optionally fields separated by DELIMITER character or characters can be parsed and stored in variables supplied in the FIELDS attribute. Attributes
The import loop continues through the file until the end is reached or the loop is terminated with the <MvIMPORTSTOP> tag. The imported line of data can be processed as needed, typically storing the information in a database. The variable g.recno is automatically created, and contains the current record number ( line number) in the input file, starting at 1. Records in data files can be terminated by line feeds (LF), carriage returns (CR), or line feed/carriage return pairs (LFCR). Data files should end with a blank line. <MvIMPORT> tags can be nested to read data from multiple files within the same block of code. Tip: Since searching in a text file is much slower than searching in a database, we recommend that if you will be reusing the data in a text file, you import it, store it in a Miva database, and perform searches on the database. This will speed up your scripts and avoid overloading your server. An alternative to <MvIMPORT> is the file_read() function, which reads and entire file into a single variable at once, from either the data or scripts folders. ExamplesDisplaying Imported dataSuppose you have a data file called movies.dat that is in the following format: This is a simple text file in which each line (or record) contains a film title, the director, and the year of release. Each section of data is called a field; the fields on each line are separated by delimiters--in this example the delimiter is the '|' (vertical bar or pipe) character. Cape Fear|Martin Scorsese|1991 The Trouble With Harry|Alfred Hitchcock|1955 Dr Strangelove|Stanley Kubrick|1963 Strange Days|Kathryn Bigelow|1995 Clerks|Kevin Smith|1994 The <MvIMPORT> tag can be used to read in this file, line by line, and process the data in some way--for example, displaying it in a table. This example will generate a table, row by row, and fill each row with data from a line of the data file. Example:<table> <tr> <th> </th> <th>Title</th> <th>Director</th> <th>Year</th> </tr> <MvIMPORT FILE="movies.dat" FIELDS="l.title,l.director,l.year" DELIMITER="|"> <tr> <td><MvEVAL EXPR="{ g.recno }">.</td> <td><MvEVAL EXPR="{ l.title }"></td> <td><MvEVAL EXPR="{ l.director }"></td> <td><MvEVAL EXPR="{ l.year }"></td> </tr> </MvIMPORT> </table> As the tag imports the data each variable is populated with a line from the file. After reading the first line the variable are populated as show. Example:l.title = Cape Fear l.director = Martin Scorsese l.year = 1991 The automatically-created variable g.recno will contain the current record (line) number, 1 After looping through each line in the file, The final table will look like this:
Filtering the imputIn the previous example, you might be interested only in movies from 1995. Using the FILTER attribute, you can specify a condition that the input data must meet. Records that don't pass the filter are not returned by You could modify the previous <MvIMPORT> as follows: Example:<MvIMPORT FILE="movies.dat" FIELDS="l.title,l.director,l.year" DELIMITER="|" FILTER_TYPE = "expression" FILTER="{ l.year EQ '1995' }"> <MvIMPORT> will now only return a line (records) when it finds the variable .year equals '1995'. Non-matching data is discarded and the next line read. No processing is done on the discarded data. The result of this import with the data file movies.dat would be:
Terminating the ImportImporting can be halted explicitly inside an <MvIMPORT> loop using the <MvIMPORTSTOP> tag. The program jumps to the code following the </MvIMPORT> tag, without reading any more input from the data file. Using the Record Number (recno)The variable recno, containing to the current line number in the file being read, is generated automatically by <MvIMPORT>. A variable named recno is also generated and updated when the program is navigating in a database, and refers to the current record number in that database. If you are using both <MvIMPORT> and a database at the same time, you should disambiguate the two recno variables by using g.recno to refer to the <MvIMPORT> recno, and db_alias.d.recno (where db_alias is the database alias) to refer to the database recno. Nested ImportsIt is possible in Miva Script to nest <MvIMPORT> tags. For example, you could have an 'outer' <MvIMPORT> that reads in a list of data file names from a 'master' data file, and an 'inner' <MvIMPORT> that, for each iteration of the outer <MvIMPORT>, reads the records of the data file whose name was read. The value of recno always refers to the record number in the current nesting level. |