Jump to content
UBot Underground

I want to move to $next list item


Recommended Posts

Hi all,

this has to be incredibly simple, but my brain is fried after losing a bot and rebuilding from scratch today.

 

I have a list of images that is created.  These images are uploaded one at a time via a loop.  At the bottom of the loop, I want to move to the next item on the list.  Easy enough right?  What command do I put in to do this? 

 

This code here, works, but it's throwing an error:   error: http://gyazo.com/0ce8dbfd9961936f06c7ba77a90f1c80

 

set list position(%images, $next list item(%images))

 

anyhelp would be appreciated.  I don't want to turn into a variable and increment unless I HAVE to, but if that's how it has to be done, how would you set up the increment of the list in the variable?

 

Many thanks!

Link to post
Share on other sites

I made an example so you can see how next list item works. You should be able to adapt the same thing to any bot. Where I have loaded html for you to see how it works, just replace with your image upload code.

clear list(%image)
add list to list(%image, $list from text("Image_1
Image_1
Image_3
Image_4", $new line), "Delete", "Global")
loop($list total(%image)) {
    set(#image, $next list item(%image), "Global")
    load html(#image)
}

Link to post
Share on other sites

$next list item is a command that works in 2 steps:

  1. retrieves the current list item (where the index was before it was invoked
  2. moves to the next list item before allowing UBS to move to the next command in the code

That means that the index is basically incremented automatically. It is the source of never-ending headache - especially for UBS newbs - because usually they mix this with looping a list one step more than allowed and they get lots of errors when trying to access a non-existent list item, when in fact, they only want to get the LAST one (but the command looks further)

 

Personally, I don't use it anywhere at all, because I always use $list item command instead, which allows ME to specify the index in the list.
 

Your code was even worse, because you were trying to SET the index (list position) by using it ($next list item) which basically, was always moving forward...
I hope you can see how that is wrong, now.

 

The best way to work with lists is to loop through their elements using $list item and at the end of the loop, use $increment to move the index one unit forward - but don't forget to set the total number of loops one item less than the result returned by $list total, because list items are zero-numbered!

 

HTH

Link to post
Share on other sites

Ok guys, thanks for all of the help.  I had forgotten to follow this topic, so I went about fixing a little differently.

The good news is that everything is working perfectly, EXCEPT, it is posting the first picture twice, and then after that works perfectly.

Here's abbreviated code so you can see what I'm doing.  Any idea why it is posting the first picture twice, and how to fix?

 

When I add set list position(%images, 1) It fixes the double post, but then I get an error at the end of the loop saying the numbers don't match up for $next list item. 

 

STUFF HAPPENS

clear list(%images)
add list to list(%images, $get files("{$special folder("Desktop")}/Pics", "Yes"), "Delete", "Global")
set list position(%images, 0)
set(#ImageNumber, $list item(%images, 0), "Global")
set list position(%images, 1)

STUFF HAPPENS

loop($list total(%images)) {

STUFF HAPPENS

    change file field(<name="file1">, #ImageNumber)
 

STUFF HAPPENS

  
    set(#ImageNumber, $next list item(%images), "Global") END OF LOOP

 

 

 

P.S.

Also, VaultBoss

The best way to work with lists is to loop through their elements using $list item and at the end of the loop, use $increment to move the index one unit forward - but don't forget to set the total number of loops one item less than the result returned by $list total, because list items are zero-numbered!  How do youse set total # of loops to one item less?  Thanks!

 

Link to post
Share on other sites

VB is just skinning a cat a different way, the simple code i posted above will do exactly what you want and will only post each list item once.

 

If you only want to flow from the start to finish of a list and only visit each list item once, then next list item works just fine. Set your list item to a var and it can be used anyware within the loop. Simply use list total to get number of loops

  • Like 1
Link to post
Share on other sites

magoo is right, of course.. many ways to skin a cat. I wasn't trying to say his isn't good.

I tend to stick to mine, because I write code that I want to re-use whenever possible. And my approach is working for me in all situations.

To keep things as close to YOUR style of coding, but w/o using $next list item, you should loop like this:

clear list(%images)
add list to list(%images, $get files("{$special folder("Desktop")}/Pics", "Yes"), "Delete", "Global")

set(#ListMax, $list total(%images), "Global")
decrement(#ListMax)

set(#ListIndex, 0, "Global")

loop(#ListMax) {
    set(#ImageNumber, $list item(%images, #ListIndex), "Global")
    change file field(<name="file1">, #ImageNumber)
    increment(#ListIndex)
}

Personally, I prefer a different code, where I loop from the end of the loop to the beginning, using LOOP WHILE instead, to have a way to force out of the loop when some conditions are met. But that would be a different discussion, on optimizing code, etc...

 

Link to post
Share on other sites

As others have said there is more than 1 way to skin a cat... but here is the way I do almost ALL of my loops.

add list to list(%testlist, $list from text("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ","), "Delete", "Global")
testloop()
define testloop {
    set(#loopcount, 0, "Global")
    loop($list total(%testlist)) {
        load html($list item(%testlist, #loopcount))
        increment(#loopcount)
    }
    set(#loopcount, 0, "Global")
}

I always use a standard variable name of #loopcount, set it to 0 just before every loop, then increment it at the end of each loop, then set it back to 0 just outside the loop (this last part is not necessary, but I always do it anyway as a 'just in case I forget one at the beginning of another loop somewhere else' kind of thing). This variable is reusable throughout my code and no need to make a special variable for every new loop you do if you create a standard variable name just for this purpose for yourself.  I then use the #loopcount variable as the list position to display or otherwise work with the $list item of the current loop.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...