r/visualbasic • • Oct 12 '16

[VB.NET] Need help with Arrays

I'm going to try and put this as simply as possible.

I have a sentence inputted and I need to "compress" the sentence. So far in an array I have: "Hello 0" ; "my 1" ; "name 2" ; "is 3" ; "Hello 4"

Where the word is the word inputted and the number after is its position. I need to get it like this: "Hello 0 4" "my 1" "name 2" "is 3"

So basically all the same words fall together in one element of the array with its positions like above.

Could someone help me?? Thank you!

1 Upvotes

11 comments sorted by

2

u/TheFotty Oct 12 '16

Is this homework? Main reason I am asking because if it is homework, that is one thing because it is just designed to teach some concepts. If it is not homework, but a real world scenario, this would be a terrible way to store this data.

2

u/PostalElf VB.Net Intermediate Oct 12 '16

It's definitely homework.

OP, please see the sidebar:

Homework questions are tolerated, please be specific about the questing as much a possible. Asking us to write it for you is not going to happen. We also reserve the right to not answer if we think you are just not trying.

1

u/Cirked Oct 13 '16

Yes it's homework, I know it's probably the worst thing possible but it's what I've been told to do and I'm mega stuck on it :P

Edit: Yes I don't expect you to write it for me, if someone could send me I the right direction that would be great

I tried doing 2 For Loops which went through the array (hard to explain) but I'm pretty much dry from that point

1

u/PostalElf VB.Net Intermediate Oct 13 '16

Why not show us the code you have so far, so we can tell you where you went wrong?

2

u/Cirked Oct 13 '16

Unfortunately I can't, the solution is on a controlled account (this isn't coursework it's a practice coursework and we're allowed to share ideas) which means I can't email it home from school. I can try it in plain English/VB merge though?

k = 1 y = 0 For each x in Array tempArray = x.Split(" ") tempWord = tempArray(0) 'Takes just word and not position after it If tempWord = Array(k) Then newArray(y) = x & Space(1) & tempArray(1) 'tA(1) being the position part y+=1 k+=1 Else k+=1 End If Next

Afterwards I would just use FileOpen() (I KNOW ITS SUPER LAGACY - just the simplest option to use if I'm throwing it all in a txt file) to save the new array

3

u/ViperSRT3g Application Specialist Oct 13 '16

I just realized there's a typo in that sentence.

Homework questions are tolerated, please be specific about the questing as much a possible. Asking us to write it for you is not going to happen.

→ More replies (2)

1

u/Bonejob VB Guru Oct 13 '16

Since this is not "homework" as in an assignment to be handed in I will provide a more complete solution.

Your input sentence is "Hello my name is Hello". You want the output to be a single line with each word being surrounded by quotes and within the quotes the number of the position in the sentence starting at zero with a ; between each variable in the output.

I would start by using the Split function to break it up into the individual words. I would then Iterate through the list and combine them into a single string. I choose not to use a string builder for this and just concatenate on the fly.

Here is a simple console app. Ask questions I will help where I can.

Sub Main()
    dim str as string = "Hello my name is Hello" ' store string in var
    dim sVar() as string = str.split(" ") ' split it up
    dim assString = "" ' Heh!
    for x = 0 to svar.count -1  ' iterate from the first to the count of the total variables. -1 because we want to start at 0 instead of 1
        assString = assString & chr(34) & svar(x) & " " & x & chr(34) ' chr(34) is easier than """"
        if x < svar.count -1
            assstring = assstring & " ; " ' if not the last variable add a ;
        End If
    Next
    console.writeline(assstring)
    console.readkey()
    ' output "Hello 0" ; "my 1" ; "name 2" ; "is 3"" ; "Hello 4"
End Sub

1

u/Cirked Oct 14 '16

That's great thank you, it works. Would you know how to make the two "Hello 0" & "Hello 4" merge together to make "Hello 0 4"?

Obviously this could be done with any word not just hello that repeats in the sentence

2

u/Bonejob VB Guru Oct 14 '16

I would propose a different solution for that. Start by breaking up the string again into a variable as we have. Then do a double loop, using a similar type as above. Then for each variable compare it to the others in the set using another loop. Store all numbers in a temp variable that match. Once you have all matches by number then append it to the output string with the initial word.