last object in a loop-index

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

Farid

Active Member
Feb 5, 2017
179
11
18
23
So basically I have a list variable, and I'm trying to find the last object in that list.


for example
there is a variable {test:::*}, {test:::*} contains 1, and 2.

I only want to see the last object's loop-index, and loop-value.
 
probably not the most efficient but you can do
code_language.skript:
set {_num} to size of {test::*}
send "{test::%{_num}%}"
 
im not sure how Farid is adding stuff to the list but if he specifies the index then it wont necessarily be a number
If he has for instance like in his examples the numbers 1 and 2 on that list, the setting the * in the list variable to the last index will give him the last value added, which is also 2 in this case. Even if he had text in the list like Potato, Crumpet, Lizard, the size of the list would be 3, and therefore {test::3} would have the value of Lizard, and the index of 3, the last item on the list.
 
If he has for instance like in his examples the numbers 1 and 2 on that list, the setting the * in the list variable to the last index will give him the last value added, which is also 2 in this case. Even if he had text in the list like Potato, Crumpet, Lizard, the size of the list would be 3, and therefore {test::3} would have the value of Lizard, and the index of 3, the last item on the list.
what im saying is that you dont have to add something to a list and let it assign a number index for you, you can specify the index like so:
code_language.skript:
set {list::ThisWillBeTheIndex} to 1
set {list::ThisWillBeAnotherIndex} to 2

loop {list::*}:
    broadcast "%loop-index% - %loop-value%
even if you did just add something to the list and let it assign a number index, it could still get screwed up if you remove values from it, for ex:
code_language.skript:
add 1 to {list::*}
add 2 to {list::*}

#at this point what you said would work... 1's loop index is 1 and 2's loop index is 2.

#however, say you remove 1 from the list then 2's index would still be 2 but the size of the list is 1 so you would be trying to get a loop value from a loop index that no longer exists
i hope i explained that well
 
what im saying is that you dont have to add something to a list and let it assign a number index for you, you can specify the index like so:
code_language.skript:
set {list::ThisWillBeTheIndex} to 1
set {list::ThisWillBeAnotherIndex} to 2

loop {list::*}:
    broadcast "%loop-index% - %loop-value%
even if you did just add something to the list and let it assign a number index, it could still get screwed up if you remove values from it, for ex:
code_language.skript:
add 1 to {list::*}
add 2 to {list::*}

#at this point what you said would work... 1's loop index is 1 and 2's loop index is 2.

#however, say you remove 1 from the list then 2's index would still be 2 but the size of the list is 1 so you would be trying to get a loop value from a loop index that no longer exists
i hope i explained that well
Ah, I see what you mean, but from my understanding he wants the loop index and value, and if the index specified is a local variable, would it not just always be the last index(since it's being set to the size) and value added when the code above it is run?
[doublepost=1501636974,1501636873][/doublepost]So what I mean is effectivly he could add or take anything he liked from the list, but when the code is run the total size of the list will be set to {_num} before it returns the variable specifying the index.
[doublepost=1501637429][/doublepost]so as long as he's checking the size of the list before he get's the {test::%{_num}%} value and index, he's good.
Hopefully I'm explaining this properly tooXD
 
let me explain this better...

Scenario 1.
You add values to {list::*} by doing:
code_language.skript:
add "value1" to {list::*}
add "value2" to {list::*}
as of now what you posted would work great... itll send "value2" since the size of the list is 2 and the index of "value2" is 2 (and the index of "value1" is 1) (keep in mind that both these indexes will never change)

But lets says you decide to:
code_language.skript:
remove "value1" from  {list::*}
now the size of the list is 1 so your code will try to send the loop-value that has the index of 1.... But Wait! Theres no loop value with the index of 1! we deleted "value1" which had the index of 1.

the only value left in the list is "value2" but its index is 2... even though the size of the list is now 1

Summary: The index of a value will never change but the size of the list could very well.

Scenario 2.
You add values to {list::*} by doing:
code_language.skript:
set {list::cool} to "value1"
set {list::awesome} to "value2"
in this scenario your code doesnt work at all because we have specified the indexes as 'cool' and 'awesome' for "value1" and "value2" respectively. So you are trying to find the loop value for a number index when no value in this list has a number index!

Summary: The index of a value may not be a number.
 
let me explain this better...

Scenario 1.
You add values to {list::*} by doing:
code_language.skript:
add "value1" to {list::*}
add "value2" to {list::*}
as of now what you posted would work great... itll send "value2" since the size of the list is 2 and the index of "value2" is 2 (and the index of "value1" is 1) (keep in mind that both these indexes will never change)

But lets says you decide to:
code_language.skript:
remove "value1" from  {list::*}
now the size of the list is 1 so your code will try to send the loop-value that has the index of 1.... But Wait! Theres no loop value with the index of 1! we deleted "value1" which had the index of 1.

the only value left in the list is "value2" but its index is 2... even though the size of the list is now 1

Summary: The index of a value will never change but the size of the list could very well.

Scenario 2.
You add values to {list::*} by doing:
code_language.skript:
set {list::cool} to "value1"
set {list::awesome} to "value2"
in this scenario your code doesnt work at all because we have specified the indexes as 'cool' and 'awesome' for "value1" and "value2" respectively. So you are trying to find the loop value for a number index when no value in this list has a number index!

Summary: The index of a value may not be a number.
but.... If you remove an item the indexes shift down. so if you, like in your first example, if you remove value 1, the size is now one, and value 2's new index is 1.
 
no they dont
[doublepost=1501638311,1501638078][/doublepost]
code_language.skript:
command /test:
    trigger:
        delete {test::*}
        add "value1" to {test::*}
        add "value2" to {test::*}
        remove "value1" from {test::*}
        loop {test::*}:
            send "%loop-index% - %loop-value%"
Untitled.png

the indexes stay for all eternity!!!!!!
 
no they dont
[doublepost=1501638311,1501638078][/doublepost]
code_language.skript:
command /test:
    trigger:
        delete {test::*}
        add "value1" to {test::*}
        add "value2" to {test::*}
        remove "value1" from {test::*}
        loop {test::*}:
            send "%loop-index% - %loop-value%"
View attachment 1029
the indexes stay for all eternity!!!!!!
OH jeez you're rightD: I understood those completely wrong...why though if it's supposed to a sequential list? Does that mean if another item is added after Value 1 it becomes index 3 or does it overwrite 2?
 
if you mean after value1 is deleted, then the next value added becomes index 1 then the next would be index 3. basically it finds the lowest available number
 
its fine lol. theres plenty of things i mix up myself.

heres my sloppy solution to @Farid's question:
code_language.skript:
set {_num} to size of {test::*}
loop {test::*}:
    remove 1 from {_num}
    if {_num} is 0:
        set {_final_index} to loop-index
        set {_final_value} to loop-value
 
  • Like
Reactions: Farid
its fine lol. theres plenty of things i mix up myself.

heres my sloppy solution to @Farid's question:
code_language.skript:
set {_num} to size of {test::*}
loop {test::*}:
    remove 1 from {_num}
    if {_num} is 0:
        set {_final_index} to loop-index
        set {_final_value} to loop-value
Thanks going to test now, I'm really bad at math, and didn't think of this solution, but basically I just need to know the last object that was added to the list variable, without removing the older ones.
 
probably not the most efficient but you can do
code_language.skript:
set {_num} to size of {test::*}
send "{test::%{_num}%}"
Not bad but to add compatibility for text indexes
code_language.skript:
last element out of all indexes of {_list::*}
that depends on the list being in order, idk if it alphabetizes indexes or something
 
  • Like
Reactions: Farid
Not bad but to add compatibility for text indexes
code_language.skript:
last element out of all indexes of {_list::*}
that depends on the list being in order, idk if it alphabetizes indexes or something
Also thanks for this, I was looking for more like this, I don't want it for alphabet, but rather what was added last, I will test this shorty!
 
Status
Not open for further replies.