Go Back  Xtreme Visual Basic Talk > General Discussion > Random Thoughts > Array within an Array: Possible?


Reply
 
Thread Tools Display Modes
  #1  
Old 08-15-2005, 08:41 AM
Blue_Drache's Avatar
Blue_Drache Blue_Drache is offline
Freshman
 
Join Date: Jan 2005
Location: Skei's Haven, Krynn
Posts: 37
Default Array within an Array: Possible?


This is just a theoretical topic and meant to encourage discussion, not a "HELP ME" topic.


NESTED ARRAYS:
Ok, a confusing subject to be sure, but I'm wondering, is it possible to store the entire contents of a single dimensional array within one element of another array. A simple example would be as follows:

Code:
Dim MyArray(5) as Variant, Test(3) as Variant Test(0) = 6 Test(1) = 10 Test(2) = 99 MyArray(0) = 150 MyArray(1) = 250 MyArray(2) = Test MyArray(3) = 400 MyArray(4) = 500

The resulting data table would look similar to this:

Code:
MyArray 1 --- 150
    |
MyArray 2 --- 250
    |
My Array 3 -------- Test --- 6
    |                 |
    |               Test --- 10
    |                 |
    |               Test --- 99
    |
My Array 4 --- 400
    |
My Array 5 --- 500
So, calling MyArray(3) would return what? An error? All three values? Some weird mishmash of unreliable data?

I believe Pearl has this ability to store arrays within arrays ... but don't quote me on that.

I also know that AutoIt has this ability, see this link to read the discussion about it, but it's more of an "undocumented and unintended feature that happens to work"
__________________
Lofting the cyberwinds on teknoleather wings, I am...
The Blue Drache
Reply With Quote
  #2  
Old 08-15-2005, 09:06 AM
BobThePenguin's Avatar
BobThePenguin BobThePenguin is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 397
Default

Well, you can do that with variants.

ie

Code:
Dim foo() As Variant Dim bar() As Variant Dim blah() As Variant ReDim foo(1 To 2) foo(1) = "Hi" foo(2) = 0.8767 ReDim bar(1 To 2) bar(1) = "Maybe" bar(2) = foo blah = bar(2) MsgBox CStr(blah(1))

And that extends to multi-dimensions too. foo could be a 5-D array. The only difference is that the line MsgBox Cstr(blah(1) would have to be MsgBox Cstr(blah(1,1,1,1,1))

All bar(2) returns is the array foo. You can do whatever you like with it.

That what you mean?
Reply With Quote
  #3  
Old 08-15-2005, 09:11 AM
Blue_Drache's Avatar
Blue_Drache Blue_Drache is offline
Freshman
 
Join Date: Jan 2005
Location: Skei's Haven, Krynn
Posts: 37
Default

Quote:
Originally Posted by BobThePenguin
Code:
Dim foo() As Variant Dim bar() As Variant Dim blah() As Variant ReDim foo(1 To 2) foo(1) = "Hi" foo(2) = 0.8767 ReDim bar(1 To 2) bar(1) = "Maybe" bar(2) = foo blah = bar(2) MsgBox CStr(blah(1))
Not exactly. Your code only sets the variant "blah" as a single dimensional arry with one element, and sets one element of the bar array to it.

I'm saying in a nested array, you'd set a single array element (in a single or multi-dimensional) equal to an ENTIRE single dimensional array.

Maybe

Code:
Dim blah(2) = Foo(1 to 2)
__________________
Lofting the cyberwinds on teknoleather wings, I am...
The Blue Drache
Reply With Quote
  #4  
Old 08-15-2005, 09:19 AM
BobThePenguin's Avatar
BobThePenguin BobThePenguin is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 397
Default

Quote:
Originally Posted by Blue_Drache
Not exactly. Your code only sets the variant "blah" as a single dimensional arry with one element, and sets one element of the bar array to it.
Check my example again, blah is a 1-D array containing 2 values. blah(1) = "Hi", blah(2) = 0.8767. You are correct that I am only setting one element of bar equal to blah. But that element of bar is the entire array foo.


ok, maybe its a little confusing, step through the example and youll see though I think.

Quote:
I'm saying in a nested array, you'd set a single array element (in a single or multi-dimensional) equal to an ENTIRE single dimensional array.
In my example, the single array element is bar(2). Then, this is being set equal to the ENTIRE single dimensional array foo.

The only point of blah was to show how you could get the information in foo using the array bar.



EDIT:

Ok, just learned a new way to deal with the arrays and its less confusing:

Code:
Dim foo() As Variant Dim bar() As Variant ReDim foo(1 To 2) foo(1) = "Hi" foo(2) = 0.8767 ReDim bar(1 To 2) bar(1) = "Maybe" bar(2) = foo MsgBox CStr(bar(2)(1)) '*

*: here bar(2)(1) gets the second element in bar and then the first element in the array that bar(2) contains; ie foo(1)
Reply With Quote
  #5  
Old 08-15-2005, 10:04 AM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

Quote:
Originally Posted by Blue_Drache
I believe Pearl has this ability to store arrays within arrays ... but don't quote me on that.
If I would quote you, I would at least write Perl correctly. (Unless you really mean the more obscure language Pearl, which seems unlikely.)

I don't know Perl, but a quick glance at some documentation seems to reveal that its arrays - unlike in the few languages that I do know - can not contain other arrays. But you can do this:
Code:
@a = (1,2,3);		
@b = ("hello", @a, "bye");    # @b is ("hello",1,2,3,"bye")
Example taken from http://www.cs.rpi.edu/~hollingd/eiw/...erlArrays.html

As I said, arrays containing arrays are not uncommon at all. In fact, in many languages all multidimensional arrays are arrays of arrays. VB is exceptional here in that it has separate multidimensional arrays. But as BobThePenguin said, it's perfectly possible to have arrays containing arrays too. But you will rarely need it.

Quote:
Originally Posted by BobThePenguin
Ok, just learned a new way to deal with the arrays and its less confusing:
Your new way is the only correct way. You can't address array elements in 'arrays of arrays' as if they were ordinary multidimensional arrays, like you were implying earlier.
Reply With Quote
  #6  
Old 08-15-2005, 10:21 AM
BobThePenguin's Avatar
BobThePenguin BobThePenguin is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 397
Default

Quote:
Originally Posted by Deadalus
Your new way is the only correct way. You can't address array elements in 'arrays of arrays' as if they were ordinary multidimensional arrays, like you were implying earlier.
If I implyed that earlier I certainly didnt mean to and I cant really see where I did. Since I've never actually had any call to use arrays like this I was only aware of how it could be done. I didnt know that you could reference the elements in the sub array like tmp(1)(1) so I just posted what occured to me at the time, thats why I had the line blah = bar(2). This does accomplish the goal of letting you view the information in bar(2), it is just a clumsy workaround when you dont know that bar(2)(1) will work.
Reply With Quote
  #7  
Old 08-15-2005, 11:07 AM
LaVolpe's Avatar
LaVolpe LaVolpe is offline
Ultimate Contributor

* Expert *
 
Join Date: Apr 2004
Location: Illinois
Posts: 2,499
Default

Another example of arrays within arrays could be use of UDTs. Here is a simple example. Usage: Rare.
Code:
Private Type ArrayUDT arrElements() As Integer ' string,long,variant,whatever End Type Private ArrayOfArrays() As ArrayUDT Private Sub SomeName() ' example only. Contain 2 arrays (100 & 200 elements) in a single array ReDim Preserve ArrayOfArrays(0 To 1) ' can also be multideminsion Dim I As Integer, J As Integer For I = 0 To UBound(ArrayOfArrays) ' redim the subarray & populate it with sample data ReDim ArrayOfArrays(I).arrElements(0 To (I + 1) * 100) For J = 0 To (I + 1) * 100 ArrayOfArrays(I).arrElements(J) = J Next Next End Sub
__________________
Insomnia is a simple byproduct of "it can't be done" {Window Shaper}
Reply With Quote
  #8  
Old 08-15-2005, 12:02 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

Sure, you can create arrays of arrays.

I mention this in the arrays installment of Standards & Practices Tutorial

In addition, a while back, in a reply to a thread on file processing, I demonstrated a use for (and how to set it up.) The example project is attached.
Attached Files
File Type: zip ArrayOfArrays.zip (1.6 KB, 23 views)
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #9  
Old 08-15-2005, 01:10 PM
by_m's Avatar
by_m by_m is offline
Contributor
 
Join Date: Jul 2004
Location: Texas
Posts: 661
Default

An array within an array is essentially a 2D array, and an array within that is a 3D array etc. The only difference is that only one element would be 2D if you only put one array element in it.
I also know that in PHP, that is how you are supposed to make multi-dimensional arrays, and you can do that just fine.
__________________
170 10101010 410 J31^170
Reply With Quote
  #10  
Old 08-15-2005, 03:45 PM
TeraBlight's Avatar
TeraBlight TeraBlight is offline
Captain Convoluted

* Expert *
 
Join Date: Jun 2005
Posts: 1,918
Default

Just to add to the confusion, I came up with an arrays-within-arrays solution to a hypothetical problem discussed in this thread
http://www.xtremevbtalk.com/showthread.php?t=227232
(post 8)

Don't look too closely at the code that follows in post 9, it's neither pretty nor efficient
__________________
"To learn without thinking is to labour in vain" - Confucius
Reply With Quote
  #11  
Old 08-15-2005, 04:50 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

Quote:
Originally Posted by by_m
An array within an array is essentially a 2D array, and an array within that is a 3D array etc
Ah, but an array of arrays allows you to have dynamic arrays of dynamic arrays,

Which, in turn allows you to redim preserve ALL array elements (and not just the last dimension...)

In addition, it allows you to conserver space in left justified sparse arrays.

For instance, suppose you have the following file that you need to copy each word into an array, AND you must maintain the array position of the word (Row and column position)

Quote:
This is a test of copying
words from
a file into an array of words maintaining the row and column positions
If you specify a 2-d array, it would have to have 13 columns and 3 rows. Unfortunately, since the first line has only 5 words, you would have 8 "empty cells" in the first row, and 11 empty cells in the second row, since you need to size the array to hold the maximum "width."

If you create a dynamic array of dynamic arrays, however, you could redim each sub-array to the correct size needed. No muss, no fuss, and no empty array elements.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown

Last edited by loquin; 08-15-2005 at 05:03 PM.
Reply With Quote
  #12  
Old 08-15-2005, 06:30 PM
by_m's Avatar
by_m by_m is offline
Contributor
 
Join Date: Jul 2004
Location: Texas
Posts: 661
Default

Truly so, good stuff. I have to say that I really like PHP arrays...and I like them even more now.
__________________
170 10101010 410 J31^170
Reply With Quote
  #13  
Old 10-20-2005, 01:25 PM
simjanes2k simjanes2k is offline
Newcomer
 
Join Date: Oct 2005
Posts: 0
Default

This is a great discussion. It reminds me of one we had over C# arrays here at work, but that turned into a heated argument, then a little yelling, and there were police dogs...

Anyway. What's the most arrays any developer would need, anyway? I've never used more than four dimensions, and that was only for a project I was handing off, and I wanted the nesting to be absolutely horrifying. Do we really need a language to offer unlimited arrays within arrays?
Reply With Quote
  #14  
Old 10-20-2005, 01:49 PM
NateO's Avatar
NateO NateO is offline
Excellent…

Forum Leader
* Expert *
 
Join Date: Jun 2004
Location: Minneapolis MN - deceased
Posts: 2,483
Default

I would be hesitant to equate a 2d array to an array of arrays, it seems to me that they’re quite different.

The following might help to show why, if you own Excel (this relies on Excel's Evaluate Method, do not attempt this specific code in VB6, Word, etc... Without binding to Excel), try the following in Excel's IDE:

Code:
Sub foo() Dim x() As Variant Let x = Array([{1,2,3;4,5,6}], _ [{"test1","test2","test3";1,2,3}]) Debug.Print x(0)(1, 2), x(0)(2, 2) Debug.Print x(1)(1, 1), x(1)(2, 1) End Sub

This gives us an a 1d array of 2d arrays, as you can see. Note that the Evaluate Method coerces the Array to a 1-based Array in the sub-arrays. And the indexing is that of the Matrix (a spreadsheet), the first array lines up as such:

1 2 3
4 5 6

So you reference your vectors as (element)(element row, element column).
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->