# Another nested loop puzzle

• 12-16-2012, 01:40 PM
RobPolley
Another nested loop puzzle
Tom,

These puzzles have been a great help to me in learning to program in Xbasic. I had not touched programming since my college FORTRAN class over 30 years ago.

I was having trouble digesting nested loops, so I made up the simplest problem I could think of and more or less copied your example to solve it. I hope to contribute to your efforts by submitting it here. The problem is:

For a list of names, write an xbasic program that counts the number of times the first letter of each name is used. Post the answer to the trace window. For a bonus, also have a message box pop up after each letter is counted with the number of times that letter has appeared. I'll submit a file of names and my code in separate attachments.

Thank you!

--Rob
• 12-21-2012, 01:52 PM
StephenP
Re: Another nested loop puzzle
Rob,

Here is my entry. Of course, I am assuming that every name starts with A thru Z. I'd have to adapt it for other circumstances.

Code:

```'Date Created: 21-Dec-2012 12:37:04 PM 'Last Updated: 21-Dec-2012 12:37:04 PM 'Created By  : spilon 'Updated By  : spilon dim name_list as c dim count_list as c dim letter_count as n name_list = "Jacqueline,Karen,Pat,Kerry,Ludivia,Martha,Doriana,Kimberly,AnnaMarie,Amanda,Caitlin,Gianna,Tara,Britt,Whitney,Kelly,Lisa,Gina,Laura,Debra,Robin,Dana,Valerie,Pamela,Steven,Thomas,Margaret,Denise,Ellen,Jack,Kimberly,Mary,Kathy,Nicole,East,Aziza,Patrick,Ryan,Caryl,Anne,Christina,Jeannette,Glenda,Mary,Laura,Marguerite,Bobble,Suzanne,Catherine,Jenifer,Anne,Cynthia,Rebecca,Heidi,Nancy,Jennifer,Ilya,Aurea,Leah,Denise,Mary Ellen,Amy ,Samantha,Laura,Stephanie,Janet,Claudia,Anne,Marta,Katherine,Martha,Susanne,Robert,Cathy,Clare,Robert,Bunny,Jennifer,Rebecca,Lauren,Danielle,Meghann,Samuel,Lauren,Laura,Julia,Christina,Mary Ann,Sylvia,Katherine,Antonia,Kathryn,Michaela,Jennifer,Michelle,Carolyn,Stacey,Joan,Tara,Kristina,Gaston,Kristine,Anne,Michelle,Kimberly,Nelen" name_list = comma_to_crlf(upper(name_list)) count_list = *for_each(foo,left(foo,1),name_list) for i = 65 to 90         letter_count = word_occurs(count_list,chr(i),crlf())         trace.writeln(chr(i) +" : " + letter_count)         ui_msg_box("Letter " + chr(i),"Count of letter " + chr(i) + ": " + letter_count) next i```
I just put the names in a variable, but getting them from a table would be no biggie. I would use something like this in place of the two lines that start with "name_list =":

Code:

`name_list = sql_records_get("::name::user_db","select upper(left(first_name,1)) from dbo.users","","")`
-- Stephen