Help with StrLength...

linum

Active Member
Licensed User
Hello. I'm working on a project that loads information from a csv file into an arraylist. I am also calculating the string lenght of each item on the arraylist and adding the lenght values together for other functions of my program. Everything works fine until my code reaches a certain point and I get an error:

"Index was outside the bounds of the array."

Here is a sample of the code that generates that error:

B4X:
       Sent1 = ArrayRefLoad.Item(0)
   Lenght1 = StrLength (Sent1)
   P1S = 0
   P1E = Lenght1
   
   Sent2 = ArrayRefLoad.Item(1)
   Lenght2 = StrLength (Sent2)
   P2S = Lenght1
   P2E = Lenght2 + Lenght1
   
   Sent3 = ArrayRefLoad.Item(2)
   Lenght3 = StrLength (Sent3)
   P3S = P2E
   P3E = Lenght3 + Lenght1 + Lenght2
   
   Sent4 = ArrayRefLoad.Item(3)
   Lenght4 = StrLength (Sent4)
   P4S = P3E
   P4E = Lenght4 + Lenght1 + Lenght2 + Lenght3
   
   Sent5 = ArrayRefLoad.Item(4)
   Lenght5 = StrLength (Sent5)
   P5S = P4E
   P5E = Lenght5 + Lenght1 + Lenght2 + Lenght3 + Lenght4

'''''''''''my code keeps increasing in this same rithm until it reaches this point:

       Sent39 = ArrayRefLoad.Item(38)
   Lenght39 = StrLength (Sent39)
   P39S = P38E
   P39E = Lenght39 + Lenght1 + Lenght2 + Lenght3 + Lenght4 + Lenght5 + Lenght6 + Lenght7 + Lenght8 + Lenght9 + Lenght10 + Lenght11 + Lenght12 + Lenght13 + Lenght14 + Lenght15 + Lenght16 + Lenght17 + Lenght18 + Lenght19 + Lenght20 + Lenght21 + Lenght22 + Lenght23 + Lenght24 + Lenght25 + Lenght26 + Lenght27 + Lenght28 + Lenght29 + Lenght30 + Lenght31 + Lenght32 + Lenght33 + Lenght34 + Lenght35 + Lenght36 + Lenght37 + Lenght38
   
   'Sent40 = ArrayRefLoad.Item(39)
   'Lenght40 = StrLength (Sent40)
   'P40S = P39E
   'P40E = Lenght40 + Lenght1 + Lenght2 + Lenght3 + Lenght4 + Lenght5 + Lenght6 + Lenght7 + Lenght8 + Lenght9 + Lenght10 + Lenght11 + Lenght12 + Lenght13 + Lenght14 + Lenght15 + Lenght16 + Lenght17 + Lenght18 + Lenght19 + Lenght20 + Lenght21 + Lenght22 + Lenght23 + Lenght24 + Lenght25 + Lenght26 + Lenght27 + Lenght28 + Lenght29 + Lenght30 + Lenght31 + Lenght32 + Lenght33 + Lenght34 + Lenght35 + Lenght36 + Lenght37 + Lenght38 + Lenght39


Can somebody help me understand what I'm doing wrong? Or is there another way to trim my code down and still make it do the same thing?


Thank you in advance...
 

Cableguy

Expert
Licensed User
Longtime User
Why not put this in a loop?

B4X:
For x= 0 to ArrayLen(ArrayRefLoad)-1
 Sent(x+1) = ArrayRefLoad.Item(x)
   Lenght(x+1) = StrLength (Sent(x+1))
   P1S = 0
   P1E = Lenght(x+1)
Next
PS:Sent and Length should be made ArrayLists for this to work..
This will allow for any length of the ArrayRefLoad
 

linum

Active Member
Licensed User
The thing is that my program then compares in between the results when I do other functions.

Here's a sample of what it does:

B4X:
            If Position < P1E AND Position >= 0 Then
               ''''''
            End If
   
            If Position < P2E AND Position >= P1E Then
               ''''''
            End If
   
            If Position < P3E AND Position >= P2E Then
               ''''''
            End If
   
            If Position < P4E AND Position >= P3E Then
               ''''''
            End If

         ''''''''''And so on up to P40E'''''''''''

Could I still use your loop to accommodate my code?
 

Cableguy

Expert
Licensed User
Longtime User
Could You please upload a simple b4p file with your main "Comparisson" function, this way we could offer you a more complete and sure to work suggestion...
But yes, it is still workable in a loop...
 

klaus

Expert
Licensed User
Longtime User
Do you have already some code ?

If yes could you upload it, so it would be easier to understand what you want to do and how.

As Cableguy already suggested, it would be simpler with array variables or ArrayList controls, even for the comparisons.

For the error "Index was outside the bounds of the array" you get, does your array really have 40 entries ? You can know it's number of entries with ArrayRefLoad.Count

To know if you can use the same loop or need another one we should know what you want to do in the If Then sequences.

Best regards.

EDIT: Hi Paulo, you were faster than me !
 

linum

Active Member
Licensed User
Thank you for your fine help. The reason why I have my code setup that way is because I am trying to trigger specific events by selecting specific text inside a textox. The way I was able to do that is by loading individual sentences (hence the code word sent1, sent2, etc.) into the arraylist and then populating the textbox with the arraylist values to load my text article. So to trigger events from inside the textbox I was able to program it so that when I position the cursor in a sentence the code compares the exact position of the whole sentence, gives me the option of selecting the complete sentence from the it's starting point all the way to the sentence period "." and does something else specific to the selected sentence.

Does this help explain things a little more?

Again, my code works perfectly, and it's very impresive to see it work but I need to create up to 100 of those sentence positions (sent98, sent99, sent100) and that's when the program gives me the error after sent39.


Thank you...
 

Cableguy

Expert
Licensed User
Longtime User
I hate to do this....

PLEASE.....upload at least a simplistic file, that include the "string handling" of your project...
We may think we understand what you want, but we can also be milles away from what you NEED..
Help us Help you.....(nice sloogan...):sign0188:
 

klaus

Expert
Licensed User
Longtime User
How do you expect us helping you without having the code to test it and reproduce the error you get.
Is your code so confidential that you are afraid to post and share it. And if so you could at least reduce it to allow us to see what happens.

Best regards.
 

linum

Active Member
Licensed User
Here's a sample that uses the same idea. The difference is that this sample is not pulling data from an arraylist but it still generates the same error.

Try the following: Place the cursor anywhere inside the textbox and right-click it. It will give you a context menu with two options so select "Replace". it will prompt you telling you what sentence you are in and after you click OK it will select that specific sentece from begining to end.

Look at the code and remove the ' signs from the following lines:

B4X:
   'Sent39 = "But since there’s nothing at all wrong with the statute that requires him to perform the ministerial task he has so far petulantly avoided, and because his malfeasance has been used to aggrieve the lawfully appointed Burris, White should be harshly condemned at the very least. "
   'Lenght39 = StrLength (Sent39)
   'P39S = P38E
   'P39E = Lenght39 + Lenght1 + Lenght2 + Lenght3 + Lenght4 + Lenght5 + Lenght6 + Lenght7 + Lenght8 + Lenght9 + Lenght10 + Lenght11 + Lenght12 + Lenght13 + Lenght14 + Lenght15 + Lenght16 + Lenght17 + Lenght18 + Lenght19 + Lenght20 + Lenght21 + Lenght22 + Lenght23 + Lenght24 + Lenght25 + Lenght26 + Lenght27 + Lenght28 + Lenght29 + Lenght30 + Lenght31 + Lenght32 + Lenght33 + Lenght34 + Lenght35 + Lenght36 + Lenght37 + Lenght38
   
   'Sent40 = "Last Sunday on Meet the Press, Reid expressed a willingness To negotiate.  But when he AND Burris meet in Washington on Wednesday, it is the new junior senator from Illinois who will show up in the stronger position, with Reid being the one who must yield lest his illicitness become too apparent To the Public, AND too distracting To legislators."
   'Lenght40 = StrLength (Sent40)
   'P40S = P39E
   'P40E = Lenght40 + Lenght1 + Lenght2 + Lenght3 + Lenght4 + Lenght5 + Lenght6 + Lenght7 + Lenght8 + Lenght9 + Lenght10 + Lenght11 + Lenght12 + Lenght13 + Lenght14 + Lenght15 + Lenght16 + Lenght17 + Lenght18 + Lenght19 + Lenght20 + Lenght21 + Lenght22 + Lenght23 + Lenght24 + Lenght25 + Lenght26 + Lenght27 + Lenght28 + Lenght29 + Lenght30 + Lenght31 + Lenght32 + Lenght33 + Lenght34 + Lenght35 + Lenght36 + Lenght37 + Lenght38 + Lenght39

Once you remove the ' signs then the error comes alive.

Again, this does not in any way reflect the code of my original project but it is enough to show you that error.



Thank you for your fine help...
 

Cableguy

Expert
Licensed User
Longtime User
Not enhancing your code, but just solving your issue, is easy..

Since every P1E is equal to the sum of all the previous values you should use:

Ie:
P2E=P2E = Lenght2 +P1E

P3E = Length3 + P2E
and so on...
 

linum

Active Member
Licensed User
Thank you so much, you are a life saver. I'll give it a shot and report back it I get stuck.


You guys rock...
 
Top