Hello All.
I'm looking for some algorithm to build a combination of strings from multiple arrays. Let me explain in detail.
- I'm working on VBA (excel). I have functions that accept one string and return a collection. These get listed in columns in excel (one column per call to the function).
- This results into arrays of strings in different columns. Unfortunately, the number of strings in an array is unknown at design time (these get retrieved from another sheet by other functions) and so are the number of columns / arrays (these depend on user interaction) and both are out of my control :-(.
- What I am supposed to do is build up a combination of all possible string values from each of these columns. I've been looking for something like an algorithm / flowchart for a headstart.
Any help towards this objective is welcome. Existing links to articles will also be fine.
Thanks in advance,
Parag
9 3758
I showed the problem description to a colleague. She said it wasn't very clear. So I thought of adding something more.
I'm not trying to look for how to populate those arrays. I want to start after they have been populated. -
Arr1 Arr2 Arr3 ... ArrN
-
S11 S21 S31 ... SN1
-
S12 S22 S32 ... SN2
-
S13 S23 S33 ... SN3
-
. . . . .
-
. . . . .
-
. . . . .
-
S1A S2B S3C ... SNX
-
kadghar 1,295
Recognized Expert Top Contributor
Hello All.
I'm looking for some algorithm to build a combination of strings from multiple arrays. Let me explain in detail.
- I'm working on VBA (excel). I have functions that accept one string and return a collection. These get listed in columns in excel (one column per call to the function).
- This results into arrays of strings in different columns. Unfortunately, the number of strings in an array is unknown at design time (these get retrieved from another sheet by other functions) and so are the number of columns / arrays (these depend on user interaction) and both are out of my control :-(.
- What I am supposed to do is build up a combination of all possible string values from each of these columns. I've been looking for something like an algorithm / flowchart for a headstart.
Any help towards this objective is welcome. Existing links to articles will also be fine.
Thanks in advance,
Parag
If i understood well, what you have is a string, lets say "Hello", and you get an array from it, lets say Arr1, where
Arr1(0) = H
Arr1(1) = e
Arr1(2) = l
Arr1(3) = l
Arr1(4) = o
and you want the list of all the possible combinations, like
H, e, l, o, He, Hl, Ho, el, eo, ll, lo Hel, Heo, Hll, Hlo, ell, and so...
Well, if im right, what you want is not a simple task. Check this thread, with a similar problem we've been discussing recently. It might be of help.
Anyway, if I didnt understand you, or the algorithm is not clear (because it is not), or you still have doubts, we'll be glad to help.
... Anyway, if I didnt understand you, or the algorithm is not clear (because it is not), or you still have doubts, we'll be glad to help.
Hahaha... yeah, when you have a complex situation like this it can be very difficult to describe in writing and get others to understand it. Let's hope we're getting somewhere near the true situation. Remember, the more clearly you can describe it to us (and the more examples we see of what is expected to happen) the more we'll be able to help.
The way I see it, you have an unknown number of columns, each containing an unknown number of entries (rows). You need to extract all the possible combinations. so in your quoted example, the combinations would be
S11S21S31...SN1
S11S21S31...SN2
S11S21S31...SN3
.
.
.
If this is anywhere close to what you're after, it shouldn't be too tough. From what you've said, these may be already in arrays. My terminology will probably get somewhat jumbled here, but I'll refer to columns and rows for (hopefully) simplicity. We can adjust things later as required. How about something along the lines of... - Determine the number of columns, c.
-
Define a numeric array ColLength with c entries.
-
Define an identical numeric array ColPos with c entries.
-
Populate ColLength array with the number of entries in each column.
-
Set all the elements in the ColPos to 1.
-
In an "infinite" loop...
-
Concatenate the cells pointed to by all the entries in ColPos array.
-
Increment entry c in your ColPos array.
-
If ColPos(c) > ColLength(c) Then
-
Set ColPos(c) back to 1
-
Increment the next ColPos entry to the left
-
End If
-
Continue this sequence all the way to the left...
-
If all ColPos entries have reached their corresponding ColLength value then
-
Exit Loop (we've done them all)
-
End If
-
End Loop
-
Does this make sense to anyone? It was straight off the top of my head.
What I'm trying to do here is actually fairly simple, just producing an array of numbers pointing to the positions to be concatenated. So the array would point to...
1,1,1...,1
1,1,1...,2
1,1,1...,3
and so on.
Hello kadghar.
No...I was looking for what Killer42 explained.
Killer42,
Thank you for this explanation. I was able to get something to work on these lines. It is not working 100% for me (but that's not a problem with the method, it is a problem with my VB code). Thank you once again.
Regards,
Parag
Glad I could help. :)
Accurately defining the problem (and the process) is often the hardest part of programming. If you can work out in enough detail what you want the program to do, it almost writes itself. (After all, that's all a program really is - a description of the process in a form simple (and consistent) enough for a compiler to understand).
How about posting what you end up with, for future reference?
Glad I could help. :)
Accurately defining the problem (and the process) is often the hardest part of programming. If you can work out in enough detail what you want the program to do, it almost writes itself. (After all, that's all a program really is - a description of the process in a form simple (and consistent) enough for a compiler to understand).
How about posting what you end up with, for future reference?
Yes. I was planning to post it....but was afraid my VB code is far too rudimentary to be posted on a public forum. Instead of the iterator method as you explained, someone told me to try a recursive approach. His approach / code is "postable" and mine is not. So for future reference, let me post this one instead [not 100% right...but close.] -
Option Explicit
-
Private Sub Command_Click()
-
-
Dim Word1 As New Collection
-
Word1.Add "Word11"
-
Word1.Add "Word12"
-
Word1.Add "Word13"
-
Word1.Add "Word14"
-
-
Dim Word2 As New Collection
-
Word2.Add "Word21"
-
Word2.Add "Word22"
-
Word2.Add "Word23"
-
Word2.Add "Word24"
-
-
Dim Word3 As New Collection
-
Word3.Add "Word31"
-
Word3.Add "Word32"
-
Word3.Add "Word33"
-
Word3.Add "Word34"
-
-
Dim WordsCol As New Collection
-
WordsCol.Add Word1
-
WordsCol.Add Word2
-
WordsCol.Add Word3
-
-
-
Dim IteratorString As Variant
-
For Each IteratorString In GenerateCombinations(vbNullString, WordsCol)
-
Debug.Print IteratorString
-
Next IteratorString
-
-
End Sub
-
-
Function GenerateCombinations(CurrentString As String, NextCollection As Collection) As Collection
-
Dim IteratorString As Variant
-
Dim ReturnCollection As New Collection
-
Dim CurrentCollection As Collection
-
Set CurrentCollection = NextCollection.Item(1)
-
If NextCollection.Count > 1 Then
-
NextCollection.Remove (1)
-
For Each IteratorString In CurrentCollection
-
Dim NextIterator As Variant
-
For Each NextIterator In GenerateCombinations(CurrentString & "-" & CStr(IteratorString), NextCollection)
-
ReturnCollection.Add NextIterator
-
Next NextIterator
-
Next IteratorString
-
NextCollection.Add CurrentCollection, , 1
-
Else
-
For Each IteratorString In CurrentCollection
-
ReturnCollection.Add CurrentString & "-" & CStr(IteratorString)
-
Next IteratorString
-
End If
-
Set GenerateCombinations = ReturnCollection
-
Set ReturnCollection = Nothing
-
Set CurrentCollection = Nothing
-
End Function
-
-
The output: -
-Word11-Word21-Word31
-
-Word11-Word21-Word32
-
-Word11-Word21-Word33
-
-Word11-Word21-Word34
-
-Word11-Word22-Word31
-
-Word11-Word22-Word32
-
-Word11-Word22-Word33
-
-Word11-Word22-Word34
-
-Word11-Word23-Word31
-
-Word11-Word23-Word32
-
-Word11-Word23-Word33
-
-Word11-Word23-Word34
-
-Word11-Word24-Word31
-
-Word11-Word24-Word32
-
-Word11-Word24-Word33
-
-Word11-Word24-Word34
-
-Word12-Word21-Word31
-
-Word12-Word21-Word32
-
-Word12-Word21-Word33
-
-Word12-Word21-Word34
-
-Word12-Word22-Word31
-
-Word12-Word22-Word32
-
-Word12-Word22-Word33
-
-Word12-Word22-Word34
-
-Word12-Word23-Word31
-
-Word12-Word23-Word32
-
-Word12-Word23-Word33
-
-Word12-Word23-Word34
-
-Word12-Word24-Word31
-
-Word12-Word24-Word32
-
-Word12-Word24-Word33
-
-Word12-Word24-Word34
-
-Word13-Word21-Word31
-
-Word13-Word21-Word32
-
-Word13-Word21-Word33
-
-Word13-Word21-Word34
-
-Word13-Word22-Word31
-
-Word13-Word22-Word32
-
-Word13-Word22-Word33
-
-Word13-Word22-Word34
-
-Word13-Word23-Word31
-
-Word13-Word23-Word32
-
-Word13-Word23-Word33
-
-Word13-Word23-Word34
-
-Word13-Word24-Word31
-
-Word13-Word24-Word32
-
-Word13-Word24-Word33
-
-Word13-Word24-Word34
-
-Word14-Word21-Word31
-
-Word14-Word21-Word32
-
-Word14-Word21-Word33
-
-Word14-Word21-Word34
-
-Word14-Word22-Word31
-
-Word14-Word22-Word32
-
-Word14-Word22-Word33
-
-Word14-Word22-Word34
-
-Word14-Word23-Word31
-
-Word14-Word23-Word32
-
-Word14-Word23-Word33
-
-Word14-Word23-Word34
-
-Word14-Word24-Word31
-
-Word14-Word24-Word32
-
-Word14-Word24-Word33
-
-Word14-Word24-Word34
-
-
Yes. I was planning to post it....but was afraid my VB code is far too rudimentary to be posted on a public forum.
I have that problem all the time. :)
Thanks for posting the code, anyway. And I'm sure we're all glad to see you got it working.
Hi,
I am quite new to VBA (sort of) and I would like to use this code in an excel macro so that the output would be on a new tab.
Can you please help?
Thanks
Francois
Hello Francois.
I believe the code is okay to be used within a VBA macro.
Regards,
Parag Doke
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bo Xu |
last post by:
Object of Combination
By Bo Xu
Introduction
A combination of n things, taken s at a time, often referred as an
s-combination out of n, is a way to select a subset of size s from a
given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald
E. Knuth gives several methods (algorithms) to generate all the
s-combinations in . In such procedure-oriented way, each
s-combination is processed while it's being generated. In some
|
by: Ross |
last post by:
Is there any combination parsing algorithm/source code available to handle
the combination problem of inputting a sequence:
YGEQLGMREMVRNCMQDL
and generating sequences:
YGEQLGMREMVRNCMQDL
YGE QLGMREMVRNCMQDL
YGE QLGMREMVR NCMQDL
|
by: AsuWoo |
last post by:
hi,
I want to implement a function that prints all possible combinations of a
characters in a string,eg. input "123"into a textbox,
add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a textbox,add
"a","b","ab"in a listbox,could anyone help me out ?
|
by: Bonj |
last post by:
I was in need of an encryption algorithm to the following requirements:
1) Must be capable of encrypting strings to a byte array, and decyrpting
back again to the same string
2) Must have the same algorithm work with strings that may or may not be
unicode
3) Number of bytes back must either be <= number of _TCHARs in *
sizeof(_TCHAR), or the relation between output size and input size can be
calculated simply. Has to take into account the...
|
by: aka_eu |
last post by:
I have this problem.
I'm trying to get all the valid combination C(N,K) that pass one
condition.
For example C(5,3) can use in any combination this numbers {1,2,3,4,5}
But let's say that I have the limitations that these pair of numbers
cannot be part of the same combination {2,3} and {4,5}
| |
by: DanielJohnson |
last post by:
how to use the combination function in python ?
For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36
Please help, I couldnt find the function through help.
|
by: pj |
last post by:
Hi,
I 'm currently writing a program that performs transliteration (i.e.,
converts greek text written using the english alphabet to "pure" greek
text using the greek alphabet) as part of my thesis. I have decided to
add the capability to convert words using some sort of lookup
algorithm as a sidekick to the "normal" conversion algorithm, and here
it starts getting interesting. I want to find an algorithm that
satisfies these...
|
by: sophia |
last post by:
Dear all,
The following is the program which i have done to find all the
combination of letters in the string "hello"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 5
|
by: Peter |
last post by:
Hi
I have a number of arrays of longs, from which I need to find a single
array which only contains the values which appear in all the original
arrays.
For example, I could have the three arrays:
1, 3, 2, 8, 5
3, 6, 1
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |