AutoHotkey Community

It is currently May 27th, 2012, 1:35 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Table/Index Look Up
PostPosted: May 13th, 2009, 12:09 am 
Offline

Joined: October 13th, 2008, 4:14 pm
Posts: 60
Location: South Park, Colorado
When fed inputs 3 and D, I need a script that can look at a table/index/array like this:

Code:
                A                       B                    C                      D                    E
1            red                 green          blue             orange       blue
2            green           red                blue             green          red
3            purple         green           red               blue             orange
...            ...                    ...                   ...                    ...                    ...
999      green           green           blue            purple         blue


and, from the table/index/array, return "blue"... the color that corresponds to row 3, column D.

Can anybody point me to a script or something that has a subroutine or function that does something like this? If not, what, in general terms, would be the best way to go about this with AHK?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 13th, 2009, 4:09 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
You should probably look at CSV functions, since AHK has more support for that format.

Here's an old one that could use an update. It's a lookup function designed to work with tables with columns both separated and padded with spaces. How about you clean it up and repost the newer version?
Code:
MyTable =
(
0                A                       B                    C                      D                    E
1            red                 green          blue             orange       blue
2            green           red                blue             green          red
3            purple         green           red               blue             orange
...            ...                    ...                   ...                    ...                    ...
999      green           green           blue            purple         blue
)
row = 999
col = D
MsgBox % LookUp( MyTable, row, col, "`n `r" )

LookUp( data, row="total", col=0, delimiters="`n, `r`t" )
{
; data = a table of data, rows are separated by the first character in 'delimiters'
; and columns are separated by *one or more of* the second character in 'delimiters'.
;
; row = the numerical index of the desired row. If the first column contains
; an integer, it will be used as both the row index and as the first column.
; use an empty string in 'row' to obtain an entire column. Use the keyword
; 'total' in this parameter to get the raw number of rows in the table
;
; col = the index or alias of the desired column. When using an alias, the topmost
; row in 'data' should contain the aliases for each column in order. Also, when using
; an alias, the topmost row's index is considered to be 0 unless the first column
; contains an integer. Use the keyword 'total' in this parameter to get the raw
; number of columns in the *first* row (a table's columns shouldn't differ row-by-row)
;
; delimiters = a string of two or more characters. The first characters defines
; the delimiter separating rows in the data table. The second character defines
; the delimiter separating columns in the data table. Other characters define
; characters which will be dropped from the beginning and end of any row or column.
   If row = total
   {
      StringSplit, data, data, % Chr(2), %delimiters%
      StringReplace, data1, data1, % SubStr(delimiters, 1, 1),, UseErrorLevel
      return ErrorLevel
   }
   Else If col = total
   {
      Loop, Parse, data, % SubStr(delimiters, 1, 1), % d2 := SubStr(delimiters, 2, 1)
         If (data1 := A_LoopField) != ""
            break
      Loop
      {
         StringReplace, data1, data1, %d2%%d2%, %d2%, All
         If ErrorLevel
            break
      }
         StringReplace, data1, data1, %d2%,, UseErrorLevel
         return ErrorLevel+1
   }
   Else   Loop, Parse, data, % d1 := SubStr(delimiters, 1, 1), % d2 := SubStr(delimiters, 2, 1)
      {
         StringSplit, data, A_LoopField, % Chr(2), %delimiters%
         If !(Thisrow := data1)
            continue
         Loop
         {
            StringReplace, thisrow, thisrow, %d2%%d2%, %d2%, All
            If ErrorLevel
               break
         }
         If !col0
            If col is NOT number
            {
               If !InStr( d2 . thisrow . d2, d2 . col . d2 )
                  Return "Error: invalid column"
               StringSplit, col, thisrow, %d2%
               RowNum = 0
               Loop, %col0%
                  If col%A_Index% = %col%
                  {
                     col = %A_Index%
                     break
                  }
            }
            Else
            {
               RowNum = 1
               StringReplace, thisrow, thisrow, %d2%, %d2%, UseErrorLevel
               col0 := ErrorLevel + 1
               If col0 < %Col%
                  Return "Error: column not present"
            }
         StringLeft, SelfIndex, Thisrow, % InStr(Thisrow, d2) - 1
         If SelfIndex is integer
            rownum := SelfIndex
         If row = %rownum%
            If col
            {
               StringSplit, data, thisrow, %d2%, % SubStr(delimiters, 3)
               return data%col%
            }
            else
               return thisrow
         else   If !row
               If col
               {
                  StringSplit, data, thisrow, %d2%, % SubStr(delimiters, 3)
                  cdata .= d1 . data%col%
               }
         rownum++
      }
   return SubStr(cdata, 2)
}

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 13th, 2009, 12:13 pm 
Offline

Joined: October 13th, 2008, 4:14 pm
Posts: 60
Location: South Park, Colorado
[VxE] wrote:
You should probably look at CSV functions, since AHK has more support for that format.

Here's an old one that could use an update. It's a lookup function designed to work with tables with columns both separated and padded with spaces. How about you clean it up and repost the newer version?

Thanks, it looks like that will suit my needs. I don't know if I've got the chops to improve it any, but if I come up with something that might be worthy of sharing, I'll post it.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, chaosad and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group