Solved

enumerating certain elements in a string array

Posted on 2011-09-21
12
155 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 250 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 250 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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