?
Solved

enumerating certain elements in a string array

Posted on 2011-09-21
12
Medium Priority
?
158 Views
Last Modified: 2012-05-12
I have an array of n string. I need a way to extract all n-1, n-2, n-3...1 string from the n strings.

I would like to do this in vb.net or sql server 2008. please help thanks
0
Comment
Question by:TrialUser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36578132
If this is an array, then you should be able to do this easily on the VB.NET side, correct? Therefore, I suspect you have a comma-delimited string. If so, then you can use a Split function.

In VB.NET, you have one built in - http://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.80).aspx.
For SQL, you can create one http://www.experts-exchange.com/Q_23786715.html; additionally, see Brandon's article - http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/A_192-Delimited-String-Parsing-in-SQL-Server-2005-and-later.html.
0
 

Author Comment

by:TrialUser
ID: 36578209
I know how to split a string. My problem is getting n-1 strings, n-2 string and so on till just 1 stirng out of all strings in the array. please help thanks
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 36578225
Did you look at the links I sent? In particular, look at the SQL question here on EE. If you know how to split a string, then you can get at n-x strings easily. Look at my example of split with position. If you have that, you essentially have the index along side the split string. You can get to what ever is needed that way. Same in VB.NET code. When you split, you are getting back an array and so have index at your disposal. Maybe if you showed with a concrete example that would help.
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36578261
If you are meaning to slowly pull apart the string, so that you go from:
A,B,C,D,E
A,B,C,D
A,B,C
A,B
A

Then you may want to look at http://msdn.microsoft.com/en-us/library/system.string.lastindexof.aspx. You basically get the LEFT() portion of the string up to the LastIndexOf(",") or whatever delimits the string.

You can do something similar in SQL.
DECLARE @str AS VARCHAR(10) = 'A,B,C,D,E';

WHILE (CHARINDEX(',', @str) > 0)
    BEGIN
    SELECT @str = LEFT(@str, LEN(@str)-CHARINDEX(',', REVERSE(@str)));
    PRINT(@str);
END

Open in new window


Kevin
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36579482
If you want to get a random selection from your string, then try this function. Give the function the number of items youwant returned from the string, and the string itself, e.g
Dim MyResult = getRandon(String.Join(",", {"apple", "pink", "lotus", "baby", "pen", "paper"}), 2)

Open in new window

Then the function
    Function getRandon(ByVal items As String, ByVal number As Integer) As String

        Dim objRandom As New Random
        Dim objCollection As New List(Of String)
        Dim objReturn As New List(Of String)
        Array.ForEach(items.Split(","), Sub(x) objCollection.Add(x))
        While objReturn.Count < number
            Dim random = objRandom.Next(0, number + 1)
            If Not objReturn.Contains(objCollection.ToList(random)) Then
                objReturn.Add(objCollection.ToList(random))
            End If
        End While
        Return String.Join(",", objReturn)
    End Function

Open in new window

0
 

Author Comment

by:TrialUser
ID: 36579853
Sorry, may be I did not explain properly. I have an array of say 5 words. I want to extract all acombinations of 4 words, 3 words, 2 words, 1 word from the array of 5 words.

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36579987
OK, I mis-understood. Here goes,
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String)) From {{3, Nothing}, {4, Nothing}, {5, Nothing}, {6, Nothing}, {7, Nothing}}
Array.ForEach(Result.Keys.ToArray, Sub(x)
                                       For Each q In theList
                                           If q.Length = x Then
                                               If Result.Item(x) Is Nothing Then
                                                   Result.Item(x) = New List(Of String) From {q}
                                               Else
                                                   Result.Item(x).Add(q)
                                               End If
                                           End If
                                       Next
                                   End Sub)

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36580011
You can then extract just the items with values using LINQ thus:
Dim FinalResult = From xd In Result Where xd.Value IsNot Nothing Select xd

Open in new window

There you have the length of the items as the key and the items in a listas thevalue of that key.
0
 

Author Comment

by:TrialUser
ID: 36580099
Could you please paste the entire code in a compilable format. Sorry I am not familiar with any of the commands you have used.

I am getting compile error right from : Array.ForEach(Result.Keys.ToArray, Sub(x)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36580193
OK I have removed the code that gives you errors,and thisis the entire routine.
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String))
For Each x In theList
    If Result.ContainsKey(x.Length) Then
        Result.Item(x.Length).Add(x)
    Else
        Result.Item(x.Length) = New List(Of String) From {x}
    End If
Next

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 1000 total points
ID: 36580202
code above had an error, here's the correct code.
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String))
For Each x In theList
    If Result.ContainsKey(x.Length) Then
        Result.Item(x.Length).Add(x)
    Else
        Result.Add(x.Length, New List(Of String) From {x})
    End If
Next

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582257
Is this question relevant anymore given that you have asked another question about combinations?
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question