最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Xojo: Array gets modified when While loop ends? - Stack Overflow

programmeradmin6浏览0评论

I had this piece of code:

While match <> Nil
  Var block As New Glyph
  block.Abstract = match.SubExpressionString(1)
  block.Noun = match.SubExpressionString(2)
  block.Verb = match.SubExpressionString(3)
  block.Doer = match.SubExpressionString(4)
  block.Place = match.SubExpressionString(5)

  ' Append the block to Glyphs
  Glyphs.Append(block)

  ' Log each match for debugging
  System.DebugLog("Matched block: " + block.Abstract + ", " + block.Noun + ", " + block.Verb + ", " + block.Doer + ", " + block.Place)

  ' Move to next match
  match = dataRegex.Search
Wend

' If Glyphs has items, populate ListBox
If Glyphs.Ubound >= 0 Then
  For Each block As Glyph In Glyphs
    ' Add a new row to the ListBox for each block
    ListBox1.AddRow(block.Abstract)

    ' Set values for each column in the row after adding the row
    ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 1) = block.Noun
    ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 2) = block.Verb
    ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 3) = block.Doer
    ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 4) = block.Place
  Next
Else
  System.DebugLog("No data found in Glyphs.")
End If

The result was that While the While/Wend loop was running, everything worked well. DebugLog displayed correct entries recognized and added to the Glyphs() array.

But when adding the entries to the ListBox from Glyphs(), it was just three copies of the latest entry, not three diffrent entries.

Raw output is this:

 "rest" found in the following glyphs:

  RESTRICTION (Abstract)
     (Noun): rule, law
     (Verb): to jail, to restrict, to halt, to stop
     (Doer): guard, warden
    (Place): prison, jailhouse

  CONSUME (Abstract)
     (Noun): mouth
     (Verb): to eat
     (Doer): eater
    (Place): restaurant, cafe

  REST (Abstract)
     (Noun): dream
     (Verb): to sleep
     (Doer): sleeper, dreamer
    (Place): bed, cot

Tried to debug it for hours, but it just wasn't working. Inside the While loop everything was working exactly like it should, but immediately after the loop ended, the Glyphs() array would get populated with three copies of "REST", and "RESTRICTION" and "CONSUME" would disappear.

I ended up just putting the ListBox code inside the While loop, like so:

While match <> Nil
  Var block As New Glyph
  block.Abstract = match.SubExpressionString(1)
  block.Noun = match.SubExpressionString(2)
  block.Verb = match.SubExpressionString(3)
  block.Doer = match.SubExpressionString(4)
  block.Place = match.SubExpressionString(5)
  
  ListBox1.AddRow(block.Abstract)
  
  ' Set values for each column in the row after adding the row
  ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 1) = block.Noun
  ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 2) = block.Verb
  ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 3) = block.Doer
  ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, 4) = block.Place
  
  ' Log each match for debugging
  'System.DebugLog("Matched block: " + block.Abstract + ", " + block.Noun + ", " + block.Verb + ", " + block.Doer + ", " + block.Place)
  
  ' Search for the next match
  match = dataRegex.Search
Wend

And it's working like it should. I'm just really curious as to why the Glyphs() approach wasn't working, and why it got reset when the While loop ended?

发布评论

评论列表(0)

  1. 暂无评论