Combine 2 Arrays Into Single Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nico3334
    New Member
    • Aug 2007
    • 28

    Combine 2 Arrays Into Single Array?

    Hi, I have 2 arrays (Each As a String), and I would like to combine these into a new single array. Is there an easy way of coding this? For example, I want "arrData" and "arrID" to be combined into a new array: "arrTotal". If anyone could give me some coding examples, I'd greatly appreciate it. Thanks!
  • VBPhilly
    New Member
    • Aug 2007
    • 95

    #2
    Originally posted by nico3334
    Hi, I have 2 arrays (Each As a String), and I would like to combine these into a new single array. Is there an easy way of coding this? For example, I want "arrData" and "arrID" to be combined into a new array: "arrTotal". If anyone could give me some coding examples, I'd greatly appreciate it. Thanks!
    This has a ArrayMerge solution that might be just what you need:

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by nico3334
      Hi, I have 2 arrays (Each As a String), and I would like to combine these into a new single array. Is there an easy way of coding this? For example, I want "arrData" and "arrID" to be combined into a new array: "arrTotal".
      Can you be more speciifc about what you mean by "merged"? Do you want the corresponding values in the arrays to be concatenated/added/multiplied/combined in some way? Do you just want the two arrays to be stuck together "end to end"? Should the values be "interleave d"? Show us some samples of what sort of thing you mean.

      Also, what version of VB are you using?

      Comment

      • nico3334
        New Member
        • Aug 2007
        • 28

        #4
        Thanks for your replies. I'm using VB 6.0. I basically am building 2 separate "tables" that are being filled with data from a SQL table. The first array bascially contains descriptive data and the 2nd array contains numerical quantitative data. I'm adding SQL data into each array using .movenext and loop. So, I have both arrays containing the data, and I would like to combine them as a new "table". So, nothing will be added or calculated, just a combined new array that contains all the data. Thanks!

        Comment

        • QVeen72
          Recognized Expert Top Contributor
          • Oct 2006
          • 1445

          #5
          Hi,

          u want a combined resultant array with Distinct Values of both the arrays..? or Simply Merge without any condition...?
          If both the array data is in 2 Tables, then u can just get the Results thru SQL Query, u dont have to Populate an array and combine,
          Some Query Like :

          [code=VB]
          Select Col1 From Table1
          Union All
          Select Col1 From Table2
          [/code]

          REgards
          Veena

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by QVeen72
            Hi,

            u want a combined resultant array with Distinct Values of both the arrays..? or Simply Merge without any condition...?
            If both the array data is in 2 Tables, then u can just get the Results thru SQL Query, u dont have to Populate an array and combine,
            Some Query Like :

            [code=sql]
            Select Col1 From Table1
            Union All
            Select Col1 From Table2
            [/code]

            REgards
            Veena

            Onemore Guesses

            [code=sql]
            SELECT (tblTableName1. MrgColumn & tblTableName2.M rgColumn) as CombColumn
            FROM tblTableName1 INNER JOIN tblTableName2 ON tblTableName1.I Dfield = tblTableName2.I Dfield[/code]

            Just explain you want to ADD one by one or merge columns?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              I'm still a bit confused about what is to be in the combined array. Could you show us just a little sample of some data, before and after.

              If the "combined" array is supposed to hold some sort of one-to-one relationship (this descriptive-text <=> this numeric-value) then perhaps the simplest would be to define an array (or collection) of a user defined type which holds the two pieces of information. For example...

              [CODE=vb]Public Type MyStuff
              DescTxt As String
              NumVal As Long
              End Type
              Dim MyArray(1 To 10) As MyStuff

              With MyArray(1)
              .DescTxt = "The first descriptive text entry"
              .NumVal = 1234
              End With

              ' And so on...[/CODE]

              Comment

              • waffles05
                New Member
                • Apr 2012
                • 1

                #8
                Originally posted by Killer42
                I'm still a bit confused about what is to be in the combined array. Could you show us just a little sample of some data, before and after.

                If the "combined" array is supposed to hold some sort of one-to-one relationship (this descriptive-text <=> this numeric-value) then perhaps the simplest would be to define an array (or collection) of a user defined type which holds the two pieces of information. For example...

                [CODE=vb]Public Type MyStuff
                DescTxt As String
                NumVal As Long
                End Type
                Dim MyArray(1 To 10) As MyStuff

                With MyArray(1)
                .DescTxt = "The first descriptive text entry"
                .NumVal = 1234
                End With

                ' And so on...[/CODE]
                I think He Means...

                Dim 1 = abc
                Dim 2 = def

                3 should be 1 joined to 2 so...
                3 = abcdef

                Comment

                • rekedtechie
                  New Member
                  • Feb 2012
                  • 51

                  #9
                  Code:
                  'testing.. :)
                  
                  'under general declarations
                  Dim arr1(),arr2(),arr3() As String
                  Dim i,n As Integer
                  
                  'let rs handles the recordset
                  
                  'sub to get records then add to a listbox
                  rs.Requery
                  n = rs.RecordCount
                  Redim arr1(1 To n)
                  Redim arr2(1 To n)
                  Redim arr3(1 To n)
                  
                  i = 1
                  Do Until rs.Eof
                  arr1(i) = rs!ID
                  arr2(i) = rs!Name
                  arr3(i) = arr1(i) & " " & arr2(i)
                  i = i + 1
                  rs.MoveNext
                  Loop
                  
                  For j = 1 To i
                  List1.add arr3(j)
                  Next j

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    You may be right waffles. However, it may also be a bit late. That last post of mine was almost 5 years ago, and the OP doesn't appear to have been active here for almost that long.

                    Comment

                    Working...