Building an html table involving count()

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Building an html table involving count()

Post by Irrlicht »

I have a macro for random map elements with a count() that produces a list of results like this: "Res 1: AAA; Res 2: AAA, BBB; Res 3: BBB", where the number of results depends on the user-inputted iterations of count().

A cleaned example of the code at present:

Code: Select all

[h: status = input(
"input|10|User input"
)]
[h: abort(status)]

[h: input= max(min(input, 25), 0)]

[h: shortList = ""]
[h: elem1 = ""]
[h: elem2 = ""]
[h: elements = ""]
[h: index = 0]

[h, count(input), code:
{
   [h: index = index + 1]
   [h, if(1d100 <= 50): elem1 = "AAA"; elem1 = ""]
   [h, if(1d100 <= 50): elem2 = "BBB"; elem2 = ""]
   [h, if(elem1 != ""): shortList = listAppend(shortList, elem1)]
   [h, if(elem2 != ""): shortList = listAppend(shortList, elem2)]
   [h, if(shortList == ""): shortList = "Null"]
   [h: elements = elements + "<font color='red'>Res " + string(index) + ": <font color='black'>" + shortList + if(index < input, "; ", "")]
   [h: shortList = ""]
}]

[r: elements]
 
Now, provided that the max number of iterations cannot go above 25, my idea was to format the output as a table that is roughly square, talking about the number of rows and cells.
I mean, if the iterations are 25, it should produce 5 rows with 5 cells each (5 x 5 = 25); if the iterations are 9, it should produce 3 rows with 3 cells each (3 x 3 = 9), and so on. In case of a number of iterations that doesn't have an integer square root, it would use the closest higher number of cells and the closest lower number of rows, with the last cells being empty (for example, 18 iterations would produce a 5 cells x 4 rows table, where the last two cells of the last row are empty; or 6 iterations would produce a 3 x 2 table... always the closest possible to a square, but not with unnecessary rows that would be fully empty).

The problem is my mind gets very messy, when I try it out... any more mathematical mind than mine who can help?
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Building an html table involving count()

Post by wolph42 »

So you want 2 for loops one for rows and one for columns, where columns is the upper sqrt and rows should not be empty thus also the upper sqrt but then with the redundant number substracted....

thats easy:

Code: Select all

col = ceil(sqrt(input))
row = col-floor((col^2-input)/col)

and then you thus have
count(row), CODE:{
  initialize row html
  count(col), CODE:{
    initialize col html
    output
    close col html
  }
  close row html
}
by the way your code can be a bit shorter:

Code: Select all

[h: abort(input(
    "input|10|User input"
))]

[h: input= max(min(input, 25), 0)]

[h: shortList    = ""]
[h: elem1        = ""]
[h: elem2        = ""]
[h: elements    = ""]
[h: index        = 0]

[h, count(input), code:
{
   [index = index + 1]
   [if(1d100 <= 50): shortList = listAppend(shortList, "AAA")]
   [if(1d100 <= 50): shortList = listAppend(shortList, "BBB")]
   [if(shortList == ""): shortList = "Null"]
   [elements = elements + "<font color='red'>Res " + string(index) + ": <font color='black'>" + shortList + if(index < input, "; ", "")]
   [shortList = ""]
}]

[r: elements] 

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Building an html table involving count()

Post by Irrlicht »

About the shortening, in the actual code, elem1 and elem2 are defined inside of a large if() that here I omitted for simplicity, so I have to keep them as they are.

Anyway, not sure how I should integrate your solution in the code, can you put them together, please?
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Building an html table involving count()

Post by wolph42 »

Code: Select all

[h: abort(input(
    "input|10|User input"
))]

[h: input= max(min(input, 25), 0)]

[h: shortList    = ""]
[h: elem1        = ""]
[h: elem2        = ""]
[h: elements    = ""]
[h: index        = 0]
col = ceil(sqrt(input))
row = col-floor((col^2-input)/col)
resultHTMLTable = "<table>"
stop            = 0

count(row), CODE:{
    resultHTMLTable = resultHTMLTable + "<tr>"
    count(col), CODE:{
        resultHTMLTable = resultHTMLTable + "<td>"
       [index = index + 1]
       [if(index > input): stop  = 1]
       
       [if(1d100 <= 50): elem1 = "AAA"; elem1 = ""]
       [if(1d100 <= 50): elem2 = "BBB"; elem2 = ""]
       [if(elem1 != ""): shortList = listAppend(shortList, elem1)]
       [if(elem2 != ""): shortList = listAppend(shortList, elem2)]
       [if(shortList == ""): shortList = "Null"]
       [if(!stop): elements = elements + "<font color='red'>Res " + string(index) + ": </font>" + shortList]
       [if(!stop): resultHTMLTable = resultHTMLTable + elements
       [shortList = ""]
        resultHTMLTable = resultHTMLTable + "</td>"
    }
    resultHTMLTable = resultHTMLTable + "</tr>"
}

resultHTMLTable = resultHTMLTable + "</table>"
 
usually I would have put in a 3rd code level to stop the internal part entirely, but only 2 code levels are allowed.
I purposefully left out brackets so its clear this is untested code.

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Building an html table involving count()

Post by Irrlicht »

I had to add an [elements = ""] before the end, otherwise each new cell would include all the content of the previous ones; other than that, it works fine and now I understood it, thanks.
(I also had to switch the formulas for Row and Col, due to the nature of rows having precedence over cells in a table, but that's a matter of personal visual taste.)

I would have needed a 3rd level of code to set elem1, 2 and so on. Without that, I'll have to put in a long list of IFs. But alright.

As a side question (trying to keep the macro as short as possible), since the actual macro also creates a list of every single element that appears in the cells, is there a quicker way to delete duplicates from a list than the foreach() I used below?
I didn't find any simple function in the wiki that would act like a "listDeleteDuplicates()" of sort.

Code: Select all

[h: elemList = ""]

--------------- This was inside the count() ---------------

      [h: elemList = listAppend(elemList, shortList)]

-----------------------------------------------------------


[h: newElemList = ""]
[h, foreach(element, elemList), code:
{
   [h, if(listContains(newElemList, element) == 0 && element != ""): newElemList = listAppend(newElemList, element)]
}]

[h: newElemList = listSort(newElemList, "N")]
Alternatively, I could have used IFs within the count(), but I don't know how shorter that could get.
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Building an html table involving count()

Post by wolph42 »

yeah, you need to check the functions in the wiki but something in the line of:

newlist = json.toList(json.unique(json.fromlist(oldlist)))

Post Reply

Return to “Macros”