Sorting a table
#1
Posted 09 February 2012 - 06:29 PM
#2
Posted 09 February 2012 - 06:59 PM
John
#3
Posted 09 February 2012 - 11:12 PM
Maybe table sorting should be supported in the platform...?
The idea of passing lists/tables as parameters to commands/functions would be really nice to have now.
#4
Posted 09 February 2012 - 11:26 PM
Are you with me?
#5
Posted 09 February 2012 - 11:57 PM
It will be a bit tricky when any of the columns in the table is a quoutation marked string, but I will try to solve that with my very recently added skills in regular expressions.
So basically the idea is as follows:
Read the table from the file as a table
Get the number of cols of the table
Clear the table
Test so that the col num u want to sort by is less than the num of cols
Read the table as a set of lists
For each element in the list {
Find the column (it is something that is delimited with commas)
copy it
prefix the list element with it
}
Perform a sort list
For each elem in the list {
Remove the prefix
}
Save the list as a file
Done!
Hmmmm... It should work. ...or not.
#6
Posted 10 February 2012 - 03:21 AM
comment("Read the table from the file as a table
Get the number of cols of the table
Clear the table
Test so that the col num u want to sort by is less than the num of cols
Read the table as a set of lists
For each element in the list \{
Find the column (it is something that is delimited with commas)
copy it
prefix the list element with it
\}
Perform a sort list
For each elem in the list \{
Remove the prefix
\}
Save the list as a file")
ui open file("Table file", #tableFile)
ui save file("Sorted Table file", #sortedTableFile)
ui text box("Sort col no>", #sortColNo)
ui drop down("Sort Order", "Ascending,Descending", #sortOrder)
ui stat monitor("numCols> ", #numCols)
ui stat monitor("item> ", #item)
set(#prefixDelimiter, "||==>>", "Global")
clear table(&myTable)
create table from file(#tableFile, &myTable)
set(#numCols, $table total columns(&myTable), "Global")
clear table(&myTable)
if($comparison(#sortColNo, "<", #numCols)) {
then {
set(#i, 0, "Global")
clear list(%tmpList)
add list to list(%tmpList, $list from file(#tableFile), "Delete", "Global")
clear list(%listToSort)
loop($list total(%tmpList)) {
set(#item, $list item(%tmpList, #i), "Global")
clear list(%parseRow)
add list to list(%parseRow, $find regular expression(#item, "(\"(?:[^\"]|\"\")*\"|[^,]*)"), "Delete", "Global")
comment("colIndex is a dirty fix since there is an error in
my regexp that is causing an extra blank line after
each list item <img src='http://ubotstudio.com/forum/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' />. Not beautyful, but it works. <img src='http://ubotstudio.com/forum/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' />
")
set(#colIndex, $eval($add($eval($multiply(#sortColNo, 2)), 1)), "Global")
set(#prefix, $list item(%parseRow, $eval($subtract(#colIndex, 1))), "Global")
set(#newPrefixedItem, "{#prefix}{#prefixDelimiter}{#item}", "Global")
add item to list(%listToSort, #newPrefixedItem, "Delete", "Global")
increment(#i)
}
clear list(%sortedList)
clear list(%tmpSortedList)
add list to list(%tmpSortedList, $sort list(%listToSort, #sortOrder), "Delete", "Global")
set(#i, 0, "Global")
loop($list total(%tmpSortedList)) {
set(#item, $list item(%tmpSortedList, #i), "Global")
set(#posAfterPrefix, $eval($add($find index(#item, #prefixDelimiter), $text length(#prefixDelimiter))), "Global")
set(#remainCharacters, $eval($subtract($subtract($text length(#item), $find index(#item, #prefixDelimiter)), $text length(#prefixDelimiter))), "Global")
set(#newItem, $substring(#item, #posAfterPrefix, #remainCharacters), "Global")
add item to list(%sortedList, #newItem, "Delete", "Global")
increment(#i)
}
save to file(#sortedTableFile, %sortedList)
clear list(%tmpList)
clear list(%listToSort)
clear list(%parseRow)
clear list(%tmpSortedList)
clear list(%sortedList)
}
else {
comment("The column to sort by does not exist.")
}
}
As mentioned in one of the comments, there is an error in my regular expression that I use to parse out the column that I want to sort by. If someone knows how to solve it (and I am sure since there are many smart ppl here), pls go ahead and post the updated regexp in this thread.
IF UB4 was able to pass list/tables as parameters >ehhh, notch, notch
Well, anyway, this works. It might not be the most beautyful code you have ever seen, but it does work.
Hmmm... It should be possible to sort by two or more columns. I'll be back.
#7
Guest_klauzser_*
Posted 10 February 2012 - 03:28 AM
Ta da!
comment("Read the table from the file as a table Get the number of cols of the table Clear the table Test so that the col num u want to sort by is less than the num of cols Read the table as a set of lists For each element in the list \{ Find the column (it is something that is delimited with commas) copy it prefix the list element with it \} Perform a sort list For each elem in the list \{ Remove the prefix \} Save the list as a file") ui open file("Table file", #tableFile) ui save file("Sorted Table file", #sortedTableFile) ui text box("Sort col no>", #sortColNo) ui drop down("Sort Order", "Ascending,Descending", #sortOrder) ui stat monitor("numCols> ", #numCols) ui stat monitor("item> ", #item) set(#prefixDelimiter, "||==>>", "Global") clear table(&myTable) create table from file(#tableFile, &myTable) set(#numCols, $table total columns(&myTable), "Global") clear table(&myTable) if($comparison(#sortColNo, "<", #numCols)) { then { set(#i, 0, "Global") clear list(%tmpList) add list to list(%tmpList, $list from file(#tableFile), "Delete", "Global") clear list(%listToSort) loop($list total(%tmpList)) { set(#item, $list item(%tmpList, #i), "Global") clear list(%parseRow) add list to list(%parseRow, $find regular expression(#item, "(\"(?:[^\"]|\"\")*\"|[^,]*)"), "Delete", "Global") comment("colIndex is a dirty fix since there is an error in my regexp that is causing an extra blank line after each list item <img src='http://ubotstudio.com/forum/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' />. Not beautyful, but it works. <img src='http://ubotstudio.com/forum/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' /> ") set(#colIndex, $eval($add($eval($multiply(#sortColNo, 2)), 1)), "Global") set(#prefix, $list item(%parseRow, $eval($subtract(#colIndex, 1))), "Global") set(#newPrefixedItem, "{#prefix}{#prefixDelimiter}{#item}", "Global") add item to list(%listToSort, #newPrefixedItem, "Delete", "Global") increment(#i) } clear list(%sortedList) clear list(%tmpSortedList) add list to list(%tmpSortedList, $sort list(%listToSort, #sortOrder), "Delete", "Global") set(#i, 0, "Global") loop($list total(%tmpSortedList)) { set(#item, $list item(%tmpSortedList, #i), "Global") set(#posAfterPrefix, $eval($add($find index(#item, #prefixDelimiter), $text length(#prefixDelimiter))), "Global") set(#remainCharacters, $eval($subtract($subtract($text length(#item), $find index(#item, #prefixDelimiter)), $text length(#prefixDelimiter))), "Global") set(#newItem, $substring(#item, #posAfterPrefix, #remainCharacters), "Global") add item to list(%sortedList, #newItem, "Delete", "Global") increment(#i) } save to file(#sortedTableFile, %sortedList) clear list(%tmpList) clear list(%listToSort) clear list(%parseRow) clear list(%tmpSortedList) clear list(%sortedList) } else { comment("The column to sort by does not exist.") } }
As mentioned in one of the comments, there is an error in my regular expression that I use to parse out the column that I want to sort by. If someone knows how to solve it (and I am sure since there are many smart ppl here), pls go ahead and post the updated regexp in this thread.
IF UB4 was able to pass list/tables as parameters >ehhh, notch, notch<, this could have been a pretty nice generic sort functionality.
Well, anyway, this works. It might not be the most beautyful code you have ever seen, but it does work.
Hmmm... It should be possible to sort by two or more columns. I'll be back.
You don't have to make it beautiful. As long as it works perfectly, that would be fine.
#8
Posted 10 February 2012 - 03:42 AM
Here is the fix.
comment("Read the table from the file as a table
Get the number of cols of the table
Clear the table
Test so that the col num u want to sort by is less than the num of cols
Read the table as a set of lists
For each element in the list \{
Find the column (it is something that is delimited with commas)
copy it
prefix the list element with it
\}
Perform a sort list
For each elem in the list \{
Remove the prefix
\}
Save the list as a file")
ui open file("Table file", #tableFile)
ui save file("Sorted Table file", #sortedTableFile)
ui text box("Sort col no>", #sortColNo)
ui drop down("Sort Order", "Ascending,Descending", #sortOrder)
ui stat monitor("numCols> ", #numCols)
ui stat monitor("item> ", #item)
set(#prefixDelimiter, "||==>>", "Global")
clear table(&myTable)
create table from file(#tableFile, &myTable)
set(#numCols, $table total columns(&myTable), "Global")
clear table(&myTable)
if($comparison(#sortColNo, "<", #numCols)) {
then {
set(#i, 0, "Global")
clear list(%tmpList)
add list to list(%tmpList, $list from file(#tableFile), "Don\'t Delete", "Global")
clear list(%listToSort)
loop($list total(%tmpList)) {
set(#item, $list item(%tmpList, #i), "Global")
clear list(%parseRow)
add list to list(%parseRow, $find regular expression(#item, "(\"(?:[^\"]|\"\")*\"|[^,]*)"), "Don\'t Delete", "Global")
comment("colIndex is a dirty fix since there is an error in
my regexp that is causing an extra blank line after
each list item <img src='http://ubotstudio.com/forum/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' />. Not beautyful, but it works. <img src='http://ubotstudio.com/forum/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' />
")
set(#colIndex, $eval($add($eval($multiply(#sortColNo, 2)), 1)), "Global")
set(#prefix, $list item(%parseRow, $eval($subtract(#colIndex, 1))), "Global")
set(#newPrefixedItem, "{#prefix}{#prefixDelimiter}{#item}", "Global")
add item to list(%listToSort, #newPrefixedItem, "Don\'t Delete", "Global")
increment(#i)
}
clear list(%sortedList)
clear list(%tmpSortedList)
add list to list(%tmpSortedList, $sort list(%listToSort, #sortOrder), "Don\'t Delete", "Global")
set(#i, 0, "Global")
loop($list total(%tmpSortedList)) {
set(#item, $list item(%tmpSortedList, #i), "Global")
set(#posAfterPrefix, $eval($add($find index(#item, #prefixDelimiter), $text length(#prefixDelimiter))), "Global")
set(#remainCharacters, $eval($subtract($subtract($text length(#item), $find index(#item, #prefixDelimiter)), $text length(#prefixDelimiter))), "Global")
set(#newItem, $substring(#item, #posAfterPrefix, #remainCharacters), "Global")
add item to list(%sortedList, #newItem, "Don\'t Delete", "Global")
increment(#i)
}
save to file(#sortedTableFile, %sortedList)
clear list(%tmpList)
clear list(%listToSort)
clear list(%parseRow)
clear list(%tmpSortedList)
clear list(%sortedList)
}
else {
comment("The sort by column no does not exist.")
}
}
#9
Posted 10 February 2012 - 05:53 PM
#10
Posted 18 February 2013 - 10:56 AM
Yea post updates this was very helpful for me. Thanks.
#11
Posted 18 February 2013 - 12:08 PM
yes the Short table function would be really handy
but there are other ways also to have the tables sorted
Edited by utsavat, 18 February 2013 - 12:09 PM.
#12
Posted 19 February 2013 - 05:18 PM
Ok, I'll remember that. No one expressed any interest in this thread when I published my code so I thought; "Hey, what the heck, I'll keep it to myself then"
There are a few updates, I just need to find the code (I had a disk crash at the beginning of this year, and I haven't used it since so... Well, there IS a backup somewhere. )
#13
Posted 20 February 2013 - 02:45 AM
Until a process is cooked up you can do this.
Load your table
build a new List with the first entry have the Table's entire row of info in that 1st List entry
Then do the same for the entire Table's contents
Then Sort the List
Save as a CSV
Then Load that CSV into a new Table or Clear the original
Buddy
#14
Posted 20 February 2013 - 03:39 AM
This is actually what my code is doing, but on top of that it adds the possibility to sort a column of your choice.
It's not very fast, but it does work.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













