View/Edit #38:from_csv

Switch to edit mode.

Name(s): from_csv
1:  "Converts a list of strings in CSV format into a 2D array of MOO strings."
2:  "Code from Digital Routes Ltd"
3:  {csv} = args
4:  array = {}
5:  for line in (csv)
6:    row = {}
7:    tokens = $su:tokenise_QnD(line, {",", "\""})
8:    quotes = 0
9:    value = ""
10:   for token in (tokens)
11:     if (token == ",")
12:       if (quotes == 0 || quotes == 2)
13:         row = {@row, value}
14:         value = ""
15:       elseif (quotes == 1)
16:         value = value + token
17:       endif
18:     elseif (token == "\"")
19:       if (quotes == 0)
20:         if (value)
21:           "Quotes in an unquoted value..."
22:           "This is an illegal CSV line; feel free to raise an error here."
23:           value = value + token
24:         else
25:           quotes = 1
26:         endif
27:       elseif (quotes == 1)
28:         quotes = 2
29:       elseif (quotes == 2)
30:         value = value + token
31:         quotes = 1
32:       endif
33:     else
34:       value = value + token
35:     endif
36:   endfor
37:   if (quotes == 1)
38:     "Quotes didn't end..."
39:     "This is an illegal CSV line; feel free to raise and error here."
40:   endif
41:   row = {@row, value}
42:   array = {@array, row}
43:   $command_utils:suspend_if_needed()
44: endfor
45: return array
46: "Last modified by Dax (#789) on Sun Oct 21 09:24:37 2001 MDT."

Verb arguments: this none this
Verb perms: Readable eXecutable NOT-Debug
Verb owned by: Hacker (#60)

You are not logged in.

[home | help | who | search | setup | code]