Hi folks! Today we’re going to dive into the warm tropical waters of basic computer science data structures! “But Seth, I thought UBot Studio required no programming experience!” That’s true, but programming knowledge, in any form, will make you a better botter. Besides, this is easier stuff, so quit complaining.
Queues and Stacks
As you already know, in UBot Studio, we have lists. In computer science, we also have lists. But computer science also gives us some variations of lists that come in handy in certain situations. One such variation is called a stack.
In a stack, you can only add new items to the top. When we add new items to the top, we “push” the item from the stack. When we “pop” an item from a stack, we’re removing the top item and returning it. You can imagine the stack of dirty magazines. When a new dirty magazine arrives in the mail, you “push” it to the top of the stack. When you’re ready to look at a dirty magazine, you “pop” the topmost magazine, read it for the articles, and promptly discard it. Or if you prefer, it’s how a cat eats pancakes.
A queue is similar, but different.
In a queue, new items are “enqueued”, meaning they are added to the back of the queue. “Dequeuing” an item from a list means removing an item from the front of the queue and returning it. Think of it like the food under the heat lamp at your local fast food repository. They operate under the First In First Out (FIFO) rule, just like a queue. This means that the rot burger made during yesterday’s shift is sold before the yum burger made during today’s shift. By using this principle, fast food repositories minimize the amount of rot that customers consume in any given day. A queue is also like a line at a bank.
Let’s take a look at how UBot Studio implements these structures. Copy this code into UBot Studio and hit run.
push(%my stack,1,"Global") push(%my stack,2,"Global") push(%my stack,3,"Global") alert($pop(%my stack)) alert($pop(%my stack)) alert($pop(%my stack))
Here’s what that should look like:
Makes sense? Good! Now let’s look at queues. Copy this code into UBot Studio and hit run.
enqueue(%my queue,1,"Global") enqueue(%my queue,2,"Global") enqueue(%my queue,3,"Global") alert($dequeue(%my queue)) alert($dequeue(%my queue)) alert($dequeue(%my queue))
And here’s what that should look like:
With Each
The advanced list section has one more interesting command: the with each command. The concept is simple, but worth explaining, as it might take some consideration to get your head around it. Or it won’t. I don’t know your head.
The with each command take two parameters, a list and a variable. When run, the command will loop once for every item in the list. Each time it loops, it fills the variable with which ever list item is in that position. Let’s look at a simple example. Copy this code into UBot Studio and hit run.
clear all data add list to list(%incredible things,$list from text("dog tower ham cereal bagel wedding ring turtle accountant upside down cat mouse rat hand stand on the moon fish harmonica",$new line),"Delete","Global") with each(%incredible things,#thing) { alert(#thing) }
And here’s what that looks like:
The with each command is also useful for changing every item in a list. Here’s how that might look:
clear all data add list to list(%incredible things,$list from text("dog tower ham cereal bagel wedding ring turtle accountant upside down cat mouse rat hand stand on the moon fish harmonica",$new line),"Delete","Global") with each(%incredible things,#thing) { add item to list(%incredible times,"the time we saw that {#thing}","Don't Delete","Global") } with each(%incredible times,#thing) { alert(#thing) }
Note that I have two with each commands. The first one takes one list and turns it into a new list. Each loop iteration adds an item to our %incredible times, adding “the time we saw that” to whatever was in %incredible things. Here’s what it looks like when you run it:
Of course, these are just two simple examples of what can be done with the with each command. You’ll find lots of ways to use “with each”, and with each one, you’ll be a better botter.