How to loop on a LIST variable
here's another quick tutorial for LoliScript.
Let's say you have a variable called
LIST123and you want to perform some action on all its elements.
Sometimes, like in the case of the
FUNCTIONblock, you can type
<LIST123[*]>in the input field and the block will be executed on all the elements of the list at the same time.
Other times you might want to perform multiple requests after scraping links e.g. after a google search.
This is how you can set up a loop in a very ASM-like fashion to get what you want:
UTILITY List "LIST123" Length -> VAR "LEN" SET VAR "INDEX" "0" WHILE "<INDEX>" LessThan "<LEN>" ## Do something with the list element like this: PRINT "<LIST123[<INDEX>]>" FUNCTION Compute "<INDEX>+1" -> VAR "INDEX" ENDWHILE
jaygit last edited by jaygit
I am only able to print last username and password but not able to print all username and passwords. have a look below:
<--- Executing Block get_Username --->
Parsed variable | Name: Username | Value: henryhk
<--- Executing Block get_pass --->
Parsed variable | Name: Password | Value: krlynch8790
<--- Executing Block FUNCTION --->
Executed function Compute on input 4+1 with outcome 5
Parsed variable | Name: INDEX | Value: 5
Jumping to line 49
Jumping to line 69
===== DEBUGGER ENDED AFTER 32.953125 SECOND(S) WITH STATUS: SUCCESS =====
I USED FOLLOWING :
##can delete the two lines below when it is certain the capture is working as intended
FUNCTION Compute "<INDEX>+1" -> VAR "INDEX"
You are using single variables, not lists. Please check my example again, I am looping on a LIST variable!
jaygit last edited by
After looping on a LIST variable, the block executed on all the elements of the list at the same time. Now I want to parse let us say "Username" and "password" from each <sources> of elements executed in the list so how to do it?
Ah ok I get it so you want to do
START LOOP MAKE REQUEST (AND GET SOURCE) PARSE USERNAME AND PASSWORD END LOOP
An easy way would be creating two list variables (USERNAMES and PASSWORDS) and adding elements to it
This is the pseudocode now
CREATE LIST USERNAMES CREATE LIST PASSWORDS BEGIN LOOP MAKE REQUEST (AND GET SOURCE) PARSE USERNAME ADD USERNAME TO USERNAMES PARSE PASSWORD ADD PASSWORD TO PASSWORDS END LOOP
To create lists you can use
UTILITY List "" Create -> VAR "USERNAMES" UTILITY List "" Create -> VAR "PASSWORDS"
To add the elements you can do:
UTILITY List "USERNAMES" Add "<Username>" "-1" -> VAR "USERNAMES" UTILITY List "PASSWORDS" Add "<Password>" "-1" -> VAR "PASSWORDS"
-1is used to add them to the end of the list.
I hope the LS syntax is correct because I didn't test it but you get the general idea.
jaygit last edited by
Thanks a lot Ruri. u r simply master.
let's say a parse block parsed a list of id, now i want to perform a request for every single item in that this, is there any way i can do this?
@Ruri please help me out.
@leecher101 that's exactly what my code is for. When i write "do something with the list element" it means instead of just printing it you can make a request with it, using
<LIST123[<INDEX>]>as the URL.
@Ruri thanks worked
Micro last edited by
Is there a way to do this multithreaded ?
In example, my scraped list contains 50 Id's and I want to loop these with multiple threads to speed it up.
Uhm not really no, but it might be a good idea for a new command ^_^
Hakka last edited by
Good Afternoon all,
So i have a variable list named Singer Area 1950 - 2000 with all the singers in it. A lot of them comes up very often.
Is it possible to extract this variable to separate like a specific Singer per List.
Example Singer Area 1950 - 2000 : AAAA-1950,BBBB-1955,CCCC-1960,AAAA-1950,BBBB-1955,CCCC-1960,
Year 1950: AAAA-1950,AAAA-1950 etc.
Year 1955: BBBB-1955,BBBB-1955 etc.
I tried with the above code but nothing happens. Any help will be very much appreciated.
SET VAR "TOSPLIT" "AAAA-1950,BBBB-1955,CCCC-1960,AAAA-1950,BBBB-1955,CCCC-1960" UTILITY Variable "TOSPLIT" Split "," -> VAR "SINGERS" UTILITY List "SINGERS" RemoveValues DoesNotContain "1950" -> VAR "1950" UTILITY List "SINGERS" RemoveValues DoesNotContain "1955" -> VAR "1955" UTILITY List "SINGERS" RemoveValues DoesNotContain "1960" -> VAR "1960"
@Hakka next time please open a separate thread as this was a completely separate issue, not related to the thread.
Hakka last edited by
Hu Ruri...Point noted...My excuse for that...I too over the code and it gives me a syntax error with the message that the code is invalid List action...
@Hakka if it tells you that, it means you are on an older version of OB. Please use latest 1.2.2