Think of something that you wish Authorware could do but it doesn't?  Let the our good friends at Macromedia know via the wishlist.

Please let us know if you find any of the materials on this site inappropriate or offensive. Please include the url and why the material should be reviewed.

Comments and questions about the site are also welcome. Please no Authorware questions, use the AWARE list.

Back

B7004 - How do I convert a Tab/Return delimited string to a 2D list?

by - Joseph Ganci


Using the following method to parse a Tab/Return separated string into a 2D List APPEARS to work OK:

myString := "item11\titem12\titem13\ritem21\titem22\titem23\ritem31\titem32\titem33"

myString := Replace("\t", "\",\"", myString)

my2DList := []

repeat with i := 1 to LineCount(myString)
   AddLinear(my2DList, List(GetLine(myString, i), ".", ",")
end repeat

OR, alternately using:

my2DList := [[],[],[]]

repeat with i := 1 to LineCount(myString)
   my2DList[i] := List(GetLine(myString, i), ".", ",")
end repeat

Then displaying/assigning the entire List or individual Lists works OK, so:

{my2DList} produces [["item11", "item12", "item13"], ["item21", "item22", "item23"], ["item31", "item32", "item33"]]

AND,

{my2DList[1]} produces ["item11", "item12", "item13"]

HOWEVER, when trying to address individual elements, AW returns nulls, zeros, or an entire secondary List, so:

{my2DList[n,n]} (where n is an integer between 1 and 3) either produces null, nitz, nada,

OR it produces the entire secondary list but without the end brackets and quotes, like this:

item11", "item12", "item13

ONE SOLUTION I've found is to convert the List to a string and back again to a List:

my2DList := String(my2DList)
my2DList := List(my2DList, ".", ",")

BUT, I REALLY HATE having to do this. It's for a web-enabled/database driven project with some large database tables/record sets. If I have to convert all my Lists back to strings and back again to Lists I'm gonna just go back to parsing the string into individual variables using EvalAssign(well, maybe - maybe not). (And I thought parsing the record sets into Lists was going to be so cool and efficient).

So what's the DEAL here? Can anyone come up with a better solution?

Whining in Wisconsin ;-)

I think the following will do what you need:

myString := "item11\titem12\titem13\ritem21\titem22\titem23\ritem31\titem32\titem33"

my2DList := Array(0, LineCount(myString), 1)

repeat with i := 1 to LineCount(myString)
   line := GetLine(myString, i)
   repeat with j := 1 to LineCount(line, "\t")
      my2DList[i][j] := GetLine(line, j, j, "\t")
   end repeat
end repeat

I've tested it and it appears to be storing and retrieving the individual values in the list correctly. Hope this helps.


Subsequently, Mike Baker presented this excellent solution:

myString := "item11\titem12\titem13\ritem21\titem22\titem23\ritem31\titem32\titem33"
myString := Replace("\t", "\", \"", myString )
myString := Replace("\r", "\"], [\"", myString )
myString := "[[\"" ^ myString ^ "\"]]"

my2DList := List( myString )


The original poster than wrote:
First of all, many thanks to Joe Ganci and Mike Baker for their solutions. Each solution solves the problem of not being able to display or retrieve individual elements of a 2D List. I tested and compared both of their solutions, and also tested my solution.

The findings:

Using a large Tab/Return separated text file/string with 11,700 characters, and checking SystemSeconds immediately before and after the code executes:

(Pentium III, 500 MHz)

Joe Ganci's solution takes an average of .23 seconds to execute.

Mike Baker's solution takes an average of .59 seconds to execute.

My solution takes an average of .57 seconds to execute.

My original method w/o the "fix" executes in an average of .3 seconds (Joe still wins by 70 milliseconds - darn! ;-)

I thought that Mike's solution would be faster, but, amazing as it seems, I WAS wrong 8-0 - (sorry for doubting you Joe). Joe's solution is actually between 2 and 3 times faster. So, using repeat loops and even nested repeat loops to fill a List is (according to my findings) significantly faster than using the Replace, String, and List functions, although one could argue that the latter solutions are simpler, and, if you're not a speed freak like me, you could go that way.

Anyway, as for me, I'll save every millisecond I possibly can and go with Joe's solution.

~worries -- in Wisconsin ;-)


I then responded:
When I saw Mike's solution, I thought, "Shoot, that's even better than mine!" I also thought it would be a lot faster than mine. Oh, well, I guess I got lucky on this one! :)

Repeat loops ARE fast in Authorware. That's why when you use them everything else in Authorware crawls, and if you get caught in an infinite loop, the only way out is to hit Ctrl-Break.


Mike Baker finally weighed in with:
No doubt about it - Joe rocks! And Authorware's string functions, uh, don't! I ran some tests and it seems that 3000 chars is the breakpoint. Less than 3000 chars and the replace & list conversion is faster. Greater than 3000 and the nested loop is faster. Beyond that number it gets exponentially worse doing the replace and list function combination.

Me again:
Some important and unexpected lessons were learned during this. No doubt that I would have expected Mike's solution to be faster than mine. I was as surpised as anyone that my solution was faster.

There are 0 reviews
Add your review
Back