Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 23rd, 2005, 08:22 AM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DolphinBay
Default Create All Possible Combinations From Varying Sets

Good morning,

This is the task I am trying to achieve in graphical format:

Set 1 - Red, Yellow, Blue
Set 2 - 8, 9
Set 3 - Covered

Result:
Red/8/Covered
Red/9/Covered
Yellow/8/Covered
Yellow/9/Covered
Blue/8/Covered
Blue/9/Covered

This is where my dilemna occurs... The number of sets, as well as the number of options within each set is determined at runtime.

I have been trying to code this for 2 days and my brain is fried. I am hoping that someone elses "eyeballs" may shed some new light on an approach.

I need to be able to create a result set that includes all possible combinations (without duplicates) of a multi-dimensional array. As stated above the number of dimensions is not known until run-time so I am not able to hard-code a specific number of loops to iterate through.

I am coding in VB.NET so I would appreciate any help be provided in the same language.

MANY MANY thanks in advance.

Regards,
Eric



Dolphin Bay, Inc. -- turning visions into eReality(tm) -- West Palm Beach, FL
Website Design, Internet Strategy, Search Engine Marketing

www.dphin.com
  #2 (permalink)  
Old April 23rd, 2005, 04:01 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This definately calls for some recursion. I don't normally provide complete solutions (unless I'm getting paid :D), but this particular challenge was interesting. Plus I tried writing out what I thought you should try, but I simply could not word it well enough. So I figured I'd just give it a go and see if I could make it work:

Dim aryJaggedData(2)() As String
aryJaggedData(0) = New String() {"Red", "Yellow", "Blue"}
aryJaggedData(1) = New String() {"8", "9"}
aryJaggedData(2) = New String() {"Covered"}

strResult = RecurseData(aryJaggedData, 0, String.Empty, String.Empty))

Private Function RecurseData(ByVal aryData As String()(), ByVal intRecurseLevel As Integer, ByVal strResult As String, ByVal strLine As String) As String
    Dim strPositionVal As String
    Dim strMyLine As String
    For i As Integer = 0 To aryData(intRecurseLevel).GetUpperBound(0)
        strPositionVal = aryData(intRecurseLevel)(i)
        If strLine.Length > 0 Then
            strMyLine = String.Format("{0}/{1}", strLine, strPositionVal)
        Else
            strMyLine = strPositionVal
        End If
        If intRecurseLevel < aryData.GetUpperBound(0) Then
            'Need to traverse down the y axis
            strResult = RecurseData(aryData, intRecurseLevel + 1, strResult, strMyLine)
        Else
            strResult = String.Format("{0}{1}{2}", strResult, Environment.NewLine, strMyLine)
        End If
    Next
    Return strResult
End Function


-Peter
  #3 (permalink)  
Old April 23rd, 2005, 04:38 PM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DolphinBay
Default

Peter,

Thank you for your rather complete response. As the concept of recursion is one that I am not all that familar with, the time now comes to take apart what you coded to see if I am figure out what is happening. Any particular resource on the Net that you could point me? Failing that, looks like I will let my fingers see what Google can pull up.

Many thanks again...

Eric


Dolphin Bay, Inc. -- turning visions into eReality(tm) -- West Palm Beach, FL
Website Design, Internet Strategy, Search Engine Marketing

www.dphin.com
  #4 (permalink)  
Old April 24th, 2005, 11:41 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here's my logic:

We start with your set of information. It exists as a jagged array which is an array of arrays (versus a 2 dimensional array which is a single array representing a square matrix).
Code:
      | Item1   Item2   Item3
------+----------------------
Set 1 | Red     Yellow  Blue
Set 2 | 8       9
Set 3 | Covered
We make the initial call to the recursing function telling it "start spinning thru all the elements in ONE set, starting at the first set".
The 0 in the second argument is what tells it to start at the first element, or element 0 in the main array. Then the recursive function adds the string of the current position in the process (set 1, item 1) to the line. Then it checks to see if there are more sets to deal with and it calls the recursive function to dive into the next set. When a recursion call returns, the function moves on to the next item in the set that it is dealing with.

Here's what the process looks like from the perspective of the set/item positions and what happens at each position.
Code:
[start process] (recurse into set 1)
Red             (recurse into set 2)
  - 8           (recurse into set 3)
    - Covered   [end of sets, no action]
  - 9           (recurse into set 3)
    - Covered   [end of sets, no action]
Yellow          (recurse into set 2)
  - 8           (recurse into set 3)
    - Covered   [end of sets, no action]
  - 9           (recurse into set 3)
    - Covered   [end of sets, no action]
Blue            (recurse into set 2)
  - 8           (recurse into set 3)
    - Covered   [end of sets, no action]
  - 9           (recurse into set 3)
    - Covered   [end of sets, no action]
Hope this makes sense to you. Wrapping your head around recursive logic can be tricky if you haven't worked with it.

-Peter
  #5 (permalink)  
Old April 26th, 2005, 07:50 AM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DolphinBay
Default

Peter,

Thank you for the explanation... after looking at the function for a while I am starting to grasp the concepts around recursion. Like you said, it is not exactly the easiest logic to get your arms around.

I am running into a problem when trying to populate the sets with dynamic data retrieved from a SQL Table. When I hard code the data into the jagged array, everything works fine. However, when I try to dynamically populate the data, it is not recursing through the information. Below is my code:

Dim sOptDtls As String = ""

'BUILD STRING TO INCLUDE IN JAGGED ARRAY
For Each dra In ds.Tables("option_details").Rows
   sOptDtls &= dra.Item("styleOptDtl_id") & ","
Next

'REMOVE TRAILING COMMA FROM STRING
sOptDtls = Left(sOptDtls, Len(sOptDtls) - 1)

'ADD STRING OF DETAILS TO aOptData
     aOptData(i) = New String() {sOptDtls}

When I check the first value in the array it is returning the entire string, rather than just the first value:
aOptData(0)(0) = "1,2,3"

Where if I hard code the data:
     aOptData(i) = New String() {1,2,3}

The first value would be returned as:
aOptData(0)(0) = "1"

Any ideas? Is it a datatype issue?

Many thanks again,
Eric


Dolphin Bay, Inc. -- turning visions into eReality(tm) -- West Palm Beach, FL
Website Design, Internet Strategy, Search Engine Marketing

www.dphin.com
  #6 (permalink)  
Old April 26th, 2005, 07:59 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The { } syntax is for literal strings separated by commas. The compiler is seeing one value: your variable (which happens to be a string with commas in it).

Use the Split method to take your string and make an array out of it.

'ADD STRING OF DETAILS TO aOptData
aOptData(i) = sOptDtls.Split(","c)

(The trailing c on the argument specifies the string literal as a char vs a string.)

-Peter
  #7 (permalink)  
Old April 26th, 2005, 08:20 AM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DolphinBay
Default

All I can say is .. DUH!

I was getting so involved in the functions, I wasn't looking at the code. Thanks again Peter.


Dolphin Bay, Inc. -- turning visions into eReality(tm) -- West Palm Beach, FL
Website Design, Internet Strategy, Search Engine Marketing

www.dphin.com
  #8 (permalink)  
Old April 26th, 2005, 10:55 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Although the String.Split method might be ok in isolated test conditions, there is always the threat of getting data that contains a character that you are splitting on (particularly a common one like ,). Instead of relying on a certain character in a concatenated string you can just build the array on the spin thru your data table. Also, concatenating a string in the way you are is not the most efficient method.

Instead, I'd recommend using the System.Collections.Specialized.StringCollection class to build your list of items from the database. It functions just like the ArrayList collection (such that you can add items on the fly) but works with the String type instead of the Object type so you don't need to do any type conversions. You'll just need to modify the recursion function to deal with that class instead of an array of strings.

-Peter


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox with varying background colors Satal Keto Access VBA 2 January 30th, 2008 11:23 AM
varying data range stealthdevil Excel VBA 6 July 27th, 2007 02:30 AM
Varying DataGrid URLs sueolo ASP.NET 1.0 and 1.1 Basics 1 September 30th, 2005 11:34 PM
Mouse-Keyboard Combinations shadowpug VB.NET 2002/2003 Basics 4 February 11th, 2004 06:21 PM
Re-Using an Array (for strings of varying lengths) WebDevel Javascript How-To 2 December 6th, 2003 06:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.