problem with two-dimensional array

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
Take the following string:

((bHc) ⊻ (bHd)) & ((bHg) → (bHi))

<colgroup><col></colgroup><tbody>
</tbody>

What the following code will do is it will put the above into the following array
sent1(1) = ((bHc) ⊻ (bHd))
sent1(2) = ((bHg) → (bHi))

I'm now trying to get the following strings into the sent2 array as so:

sent2(1,2) = (bHc)
sent2(1,2) = (bHd)
sent2(2,1) = (bHg)
sent2(2,2) = (bHi)

What I'm doing wrong I think is I have to reDim Preserve more than one thing but I can't figure out what.

Before I loop through and go to n = 2 the sent2 array is fine, meaning it appears as

sent2(1,2) = (bHc)
sent2(1,2) = (bHd)

But once I go to n = 2, the following happens.

sent2(1,2) = empty

I can then populate the next array as I should, meaning

sent2(2,1) = (bHg)
sent2(2,2) = (bHi)

But sent2(1,2) is still empty.

Sorry, if I'm not being clear but I've got to get back to work, since I'm doing this on the job.




Code:
 Dim x As Integer, y As Integer, z As Integer, b As Integer
Dim total As Integer, paren_closure As Integer, marker As Boolean
   
   
   Dim temp_string As String
       
        For x = InStr(instring, "(") To Len(instring) Step 1
           
           temp_string = Mid(instring, x, 1)
           
            If Mid(instring, x, 1) = "(" Then
            
                If marker = False Then
                z = x
                marker = True
                End If
                
            total = total + 1
            ElseIf Mid(instring, x, 1) = ")" Then
            total = total - 1
              
        
                If total = 0 Then
                marker = False
                b = b + 1
                paren_closure = x
                ReDim Preserve sent1(b)
                sent1(b) = Mid(instring, z, (x - z) + 1)
                End If
            End If
        Next
  
 Dim temp_sent1 As String, n As Integer
total = 0
marker = False






 For n = 1 To UBound(sent1)
 temp_sent1 = sent1(n)
 temp_sent1 = Mid(temp_sent1, 2, Len(temp_sent1) - 2)
 b = 0
    For x = 1 To Len(temp_sent1)
           
           
           
           temp_string = Mid(instring, x, 1)
           
            If Mid(temp_sent1, x, 1) = "(" Then
            
                If marker = False Then
                z = x
                marker = True
                End If
                
            total = total + 1
            ElseIf Mid(temp_sent1, x, 1) = ")" Then
            total = total - 1
              
        
                If total = 0 Then
                marker = False
                b = b + 1
                paren_closure = x
                ReDim Preserve sent2(1 To UBound(sent1), b)
                sent2(n, b) = Mid(temp_sent1, z, (x - z) + 1)
                End If
            End If
        Next
Next
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
thanks for the advice but I'm pretty sure that's not the problem. Further, that would be impractical because I have about 40 other functions and subs in this module and I can't now go back and change to option base 1. The problem I think has something to do with reDim Preserve. I think I'm only preserving one array but I need to preserve two.
 
Upvote 0
Here is the problem broken down to its bare essentials. We have two arrays:


dim sent1()
dim sent2()


dim n as integer, b as integer, x as integer
dim temp_sent as string




b = 0
For n = 1 to ubound(sent1)

temp_sent = sent1(n)
for x = 1 to len(temp_sent1)


code




if a then
b = b + 1
redim preserve (sent2(1 to ubound(sent1), b)
sent2(n,b) =
Code:
	next
next


What my problem is that when I redim the b I'm doing it correctly I think.
 
Upvote 0
The problem is that you're resetting b to 0 in the following loop, so that you when you increment by 1, you can extract sent2(n, 1) and sent2(n, 2).

Code:
For n = 1 To UBound(sent1)
    temp_sent1 = sent1(n)
    temp_sent1 = Mid(temp_sent1, 2, Len(temp_sent1) - 2)
    b = 0

But you're also using b here:

ReDim Preserve sent2(1 To UBound(sent1), b)

so on the first iteration when b is 1, you're shrinking the size of the array sent2().
 
Last edited:
Upvote 0
In this example, you have a nice neat 2 x 2 solution. Is it possible you may have a formula like this:

(A)((B)((C)(D)(E)))

in which case you'd need recursion to drill down through the formula and identify its structure, e.g. so that the elements could be stored in sent(1 to 2, 1 to 2, 1 to 3), or perhaps in a jagged array?
 
Upvote 0
In this example, you have a nice neat 2 x 2 solution. Is it possible you may have a formula like this:

(A)((B)((C)(D)(E)))

in which case you'd need recursion to drill down through the formula and identify its structure, e.g. so that the elements could be stored in sent(1 to 2, 1 to 2, 1 to 3), or perhaps in a jagged array?

Thanks for your help but I don't understand what you mean. As far as the b integer is concerned I don't see how I can stop resetting it to zero because when I move down to sent2(2,1), the b has to = 1.
 
Upvote 0
On your first pass through this loop, when n = 1

For n = 1 To UBound(sent1)

you set sent2(1,1) = "(bHc)" and sent2(1,2) = "(bHd)". So far, so good.

On your second pass, when n = 2, you reset b to 0, and then to 1 when you identify the element that will become sent2(2,1).

But when you do this:

ReDim Preserve sent2(1 To UBound(sent1), b) and b is 1

you destroy sent2(1,2).

When you Redim again, i.e. when b=2, sent2(1,2) will be null.

If you can count on there always being 2 elements in the second dimension, you can Redim sent2(1 to N, 1 to 2) once only, i.e. you won't need to redim for every iteration of N.
 
Upvote 0
Ok, I was able to come up with a workable solution but using the following syntax:

dim sent2(5,5)
sent2(n, b) = Mid(temp_sent1, z, (x - z) + 1)

But now I have a lot of unused spaces in the array. Can't I get rid of them with redim? What is the syntax for redim?
 
Upvote 0
You can use Redim Preserve to resize an array (bigger or smaller) whilst preserving the contents, but only on the last dimension of the array. So if you declare: Dim sent2(1 to 5, 1 to 5) you can't resize the bold dimension (without writing to another array element by element).

Having unused parts of the array is not necessarily a problem, provided you track which bits you need. In my hypothetical example in post #6, I could for example, have

ReDim sent3(1 to 2, 1 to 2, 1 to 3)

even though I only really need:

sent3(2,2,1) = (C)
sent3(2,2,2) = (D)
sent3(2,2,3) = (E)

But the best solution for you will need to take into account what you intend doing next with sent1 and sent2?
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,947
Members
448,534
Latest member
benefuexx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top