Count Lines in a String

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Count Lines in a String

Post by Dominic_Fichera » Mon Dec 29, 2014 6:47 am

Hey everyone!

I'm trying to make a script where a user can add "names" to a list and it will randomly pick a result... Sort of like a lotto.

Image

I'm trying to work out a way for my script to work out how many names have been added. Can you think of a way to count the number of lines in a variable.

For example, if my variable was:

Name 1
Name 2
Name 3

Could my script work out there were 3 names here so I could then make it pick a number between 1-3. (Once I know how to count the lines, I should be right with the rest! :)

Thanks in advance for your help! :)

Dominic Fichera

Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Re: Count Lines in a String

Post by Dominic_Fichera » Mon Dec 29, 2014 7:02 am

*UPDATE*

I've been able to answer my own question by digging through some old forum posts and using the separate function! :)

As an additional question, is there anyway to now decide "okay, my random number is 2, what is sitting on line 2 of the combo box"?

Thanks again! :)

Dominic Fichera

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Count Lines in a String

Post by Marcus Tettmar » Mon Dec 29, 2014 10:24 am

Same way really. Get list of items, use Separate to get into an array, look up item at random position:

Code: Select all

//get a random number between 1 and 3
Random>2,randnumber
Add>randnumber,1

//get the list of items from combobox
GetDialogProperty>Dialog1,MSComboBox1,ListText,itemList
Separate>itemlist,CRlF,item_array

//get item at random number position
Let>random_item=item_array_%randnumber%
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Re: Count Lines in a String

Post by Dominic_Fichera » Mon Dec 29, 2014 1:56 pm

Thanks Marcus! It's up and running now!

Appreciate your help :)

Kind regards,

Dominic Fichera

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts