winwell 1 Posted July 31, 2015 Report Share Posted July 31, 2015 I have a bot that collects values from lots of sites. At the moment I am just creating a variable for each one and assigning the value for a site to a variable, so I will have lots of them (around 100) Is there a quick and efficient way to add all those variables up to get a total sum? Using eval and add is messy when you're adding more than a few up? Thanks Quote Link to post Share on other sites
Bot-Factory 602 Posted July 31, 2015 Report Share Posted July 31, 2015 I have a bot that collects values from lots of sites. At the moment I am just creating a variable for each one and assigning the value for a site to a variable, so I will have lots of them (around 100) Is there a quick and efficient way to add all those variables up to get a total sum? Using eval and add is messy when you're adding more than a few up? ThanksI would use a table with 2 columns. 1 column the site name and second column the data. Then you can loop though the table and add the values from column 2. Dan 1 Quote Link to post Share on other sites
Code Docta (Nick C.) 638 Posted July 31, 2015 Report Share Posted July 31, 2015 Here is a cool way to do it with Ubots Python. The first to nodes are just an example of how to do it. add list to list(%numbers,$list from text("1,2,3,4,5",","),"Delete","Global") alert($run python with result("list_to_sum = ({$text from list(%numbers,",")}) the_Sum = sum(list_to_sum) the_Sum")) divider comment("the function") comment("funtion takes text") alert($Sum a List("1,2,3,4,5", ",")) comment("function takes a list") alert($Sum a List(%numbers, $new line)) define $Sum a List(#LIST_FROM_TEXT, #DELIMITER) { return($replace regular expression($run python with result("list_to_sum = ({$text from list($list from text(#LIST_FROM_TEXT,#DELIMITER),",")}) the_Sum = sum(list_to_sum) the_Sum"),"(|)",$nothing)) } Then I made a function to take either some test or Ubot list. The first line of python takes ubot list and makes it into an Python list. The second uses the sum() func of python and sticks it an variable. Last line gives the result. in Python 2.7 the last line would look like thisprint the_Sum It is easier than it looks all by it self as a function. Only good for Ubot 5 Devs. Hope that helps, CD 1 Quote Link to post Share on other sites
winwell 1 Posted August 1, 2015 Author Report Share Posted August 1, 2015 Thanks both, Will try these both out a bit later and report back - cheers Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.