Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Table doesn't load into an object



  • Please log in to reply
3 replies to this topic
Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008
I want to transform my table into an object and then traverse the object. What am I doing wrong here?
aimals =
(
rat	grey	small
dog	black	big
bear	black	huge
whale	blue	huge
)

animals := []
Loop, parse, file, "`n"
   {
   Row := A_Index
   Loop, parse, A_LoopField, "`t"
      animals[%Row%,%A_Index%] := A_LoopField
   }

For RowNumber, Row in animals
   {
   For ColumnNumber, item in Row
      MsgBox % "Item " .  RowNumber . " , " . ColumnNumber . " is " . %item%
   }


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
✓  Best Answer
table =

(

rat`tgrey`tsmall

dog`tblack`tbig

bear`tblack`thuge

whale`tblue`thuge

)





Animals := []

Loop, parse, table, `n

{

   RowNum := A_Index

   Animals.Insert(RowNum, [])   ; each row is sub-object in Animals object. You can also use just: Animals.Insert([])

   Loop, parse, A_LoopField, `t

      Animals[RowNum].Insert(A_LoopField)

}





For RowNum, RowSubObj in Animals       ; each row is sub-object in Animals object

{

   For ColNum, Value in RowSubObj

      MsgBox % ColNum ". column in " RowNum ". row:`n" Value

}





MsgBox % "Second row, third column:`n" Animals[2,3]


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
See also Table() function:
String =
(
rat`tgrey`tsmall
dog`tblack`tbig
bear`tblack`thuge
whale`tblue`thuge
)

Object := Table(String)      ; creates 2-D object (table) from string
MsgBox % Object[4,2]         ; get value of 4. row, 2. column

BackToString := Table(Object) ; creates string from 2-D object (table)
MsgBox % BackToString


Table(StringOrObj, RowsDelimiter="`n", ColumnsDelimiter="`t", OmitChars = "`r ") {  ; by Learning one. http://www.autohotkey.com/community/viewtopic.php?p=566880#p566880
	if IsObject(StringOrObj) {  ; than it is an object we want to transform to string  
		For RowNum, RowSubObj in StringOrObj
		{
			For ColNum, Value in RowSubObj
				ThisRow .= Value ColumnsDelimiter
			String .= SubStr(ThisRow, 1, StrLen(ThisRow) - StrLen(ColumnsDelimiter)) RowsDelimiter, ThisRow := ""
		} ; Q: why not RTrim()? A: Because cell may be blank and we don't want to trim other cell's delimiter
		return SubStr(String, 1, StrLen(String) - StrLen(RowsDelimiter))
	}
	else {                       ; than it is a string we want to transform to object 
		obj := []
		Loop, parse, StringOrObj, %RowsDelimiter%, %OmitChars%
		{
			RowNum := A_Index,  obj.Insert([])
			Loop, parse, A_LoopField, %ColumnsDelimiter%, %OmitChars%
				obj[RowNum].Insert(A_LoopField)
		}
		return obj
	}
}

My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Benny-D
  • Members
  • 1485 posts
  • Last active: Sep 27 2016 11:23 PM
  • Joined: 29 Feb 2008
WOW!!! Thank you very much Learning one. The function is amazing!!!