-1

I am building a web application where it has text sets.

eg.

Mayan Civilization - TextSet1

The Maya civilization shares many features with other Mesoamerican civilizations due to the high degree of interaction and cultural diffusion that characterized the region. Advances such as writing, epigraphy, and the calendar did not originate with the Maya;

Maya Influence - TextSet2

however, their civilization fully developed them. Maya influence can be detected from Honduras, Belize, Guatemala, and western El Salvador to as far away as central Mexico, more than 1,000 km (620 mi) from the Maya area. Many outside influences are found in Maya art and architecture, which are thought to result from trade and cultural exchange rather than direct external conquest.

Question

Each TextSet has a unique index since they are stored in a Gridview. The code I have loops through each item or index in the GridView by first storing the first item which is TextSet1 in a string.

Then, another For loop which loops through that string in order to count how many matches are there of the word Maya.

For Each item As GridViewRow In Me.GridViewSearchResult.Rows
        Dim txtStr as Label = DirectCast(item.FindControl("TextSet1"), Label) 
        Dim Str = txtStr.Text.toString 

    For i as Integer = 0 to Str.Length - 1

    Dim count As new Integer 
    count = Regex.Matches (Str, "Maya", RegexOptions.IgnoreCase).Count

    Next
 Next

What I am trying to achieve is that in the start, the program starts by handling TextSet1 or Str and then counts using count how many occurrences of the word Maya in Str. On the first occurrence of Maya in Str, store the entire set in the database and then exit or stop the loop. This means that the program will not move to TextSet2. I thought of performing a check outside the loop and then calling the values, however Str is declared within the For loop. Also tried to do a Regex.match outside the loop, but what I need is to stop the loop on the first occurance of the word maya in the Str currently in that loop. Any suggestions or thoughts of how can achieve that ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
HShbib
  • 1,811
  • 3
  • 25
  • 47
  • `Exiting a loop on first Regex.match and do something else` - Use [Continue statement](http://msdn.microsoft.com/en-us/library/801hyx6f(v=vs.110).aspx) – Habib Jan 09 '13 at 11:24
  • This seems like a strange setup... I guess you fetch the texts from somewhere (e.g. a database)? Why not count the words in the way you wish at the time when you fetch the texts? – mortb Jan 09 '13 at 11:31

2 Answers2

0

Implemented a new For loop similar to the first one and had a while loop which checks if count is not nothing, then if true Insert to database, then exit the For loop using Exit For.

HShbib
  • 1,811
  • 3
  • 25
  • 47
-1

Judging by the example you have given and your description of the problem I would suggest your problems are many. I would suggest that your best bet is to find a good software developer that can sit next to you and talk you through it rather than asking questions here.

However the keyword to exit a for loop in Visual Basic is "Exit For" however this will only break out of the innermost loop. To break out of all nested loops requires something a bit more complex.

This question should give you some pointers: Breaking/exit nested for in vb.net

Some comments on the code example:

For Each item As GridViewRow In Me.GridViewSearchResult.Rows ' Ok
    Dim txtStr as Label = DirectCast(item.FindControl("TextSet1"), Label) ' Ok

    Dim Str = txtStr.Text.toString ' it would be better to declare the type of Str and leave out the .toString. Dim Str as String = txtStr.Text    

    For i as Integer = 0 to Str.Length - 1 ' For every character in the string, why?   
                                           ' Note where is i used within the loop?

    Dim count As new Integer ' Declared afresh for every itteration of the 
                             ' loop and then not used. Probably not what is wanted.

    count = Regex.Matches (Str, "Maya", RegexOptions.IgnoreCase).Count ' Seems ok, but why repeat multiple times?

    ' Neither count nor i have been used.
    Next
 Next

A proposed solution:

For Each item As GridViewRow In Me.GridViewSearchResult.Rows
    Dim txtStr as Label = DirectCast(item.FindControl("TextSet1"), Label)
    Dim Str as String = txtStr.Text    
    Dim count As new Integer = Regex.Matches(Str, "Maya", RegexOptions.IgnoreCase).Count
    If count > 0 then 
       Call StoreInDb(count, str)
       Exit For
    End If
 Next
Community
  • 1
  • 1
Martin Brown
  • 24,692
  • 14
  • 77
  • 122