Results 1 to 9 of 9

Thread: Help on Union of Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    5

    Help on Union of Arrays

    So I have a problem that says: Write a program that creates a file containing the names of every person who served as either vice-president or president. The program also should display the number of those names in a message box. I also have to create a third array that names every person that served as both. That is where I am having the problem. The rest of it I have figured out. The union of the array is not working. Here is my code:

    ublic Class frmAhearnAh
    Dim vicePres() As String = IO.File.ReadAllLines("VPres.txt")
    Dim presidents() As String = IO.File.ReadAllLines("USPres.txt")
    Private Sub btnOr_Click(sender As Object, e As EventArgs) Handles btnOr.Click
    Dim eitherOR() As String
    eitherOR = (vicePres.Union(presidents))
    IO.File.WriteAllLines("OR.txt", eitherOr)
    Dim number = eitherOr.Count - 1
    MsgBox(number & " people", "File Created")
    End Sub
    End Class

    Thank you for your help in advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help on Union of Arrays

    You have forgotten your primary school maths. What you want is the intersection of the two sets, i.e. the elements common to both, rather than the union of the two sets, i.e. the elements that are in either one or the other or both.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    5

    Re: Help on Union of Arrays

    Actually no, I am looking for those who served as vice president or president, which would mean everyone on the list, except for duplicates

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help on Union of Arrays

    Quote Originally Posted by JPA21 View Post
    Actually no, I am looking for those who served as vice president or president, which would mean everyone on the list, except for duplicates
    For some reason I thought I'd read that you said "both" in your post but I see now that I just made that up.

    In that case, I'll go back to what I originally was going to post but deleted. It's NEVER adequate to say that something isn't working. ALWAYS explain what you expect to see, what you actually see and how they differ. That way, we know what we're looking for rather than just hoping something jumps out at us.

    That said, I don't understand why you're subtracting 1 from the count there. That's exactly the sort of action that should be accompanied by an explanatory comment, i.e. WHY you are seemingly inexplicably subtracting 1 from the count when what you want is the count.

    Also, while it won't stop your code working, you should not be using `eitherOr` twice like that. Union uses deferred execution, as so many LINQ methods do, which means that the result is not created when you call Union but when you use that result. By using the result twice, you're actually creating the union twice, which is inefficient. If you want to use the result of a LINQ query multiple times then you should call ToArray or ToList to create an array or collection and then use that multiple times. That way, you won't be performing the query over and over just to get the same result every time.

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Help on Union of Arrays

    Quote Originally Posted by jmcilhinney View Post
    For some reason I thought I'd read that you said "both" in your post but I see now that I just made that up.
    Quote Originally Posted by JPA21 View Post
    I also have to create a third array that names every person that served as both. That is where I am having the problem. The rest of it I have figured out. The union of the array is not working.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Help on Union of Arrays

    I second EG's confusion.
    My usual boring signature: Nothing

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Help on Union of Arrays

    Third.... in favor.... Aye!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Help on Union of Arrays

    None of the "solution" seems to jibe with the statement of work.
    Requirement: Write a program that creates a file containing the names of every person who served as either vice-president or president.
    Interpretation: Write a program that creates one file containing the names of every person who has served as either vice-president or president.
    The code is not creating a file, it is reading from two files that must have been previously created.
    Code:
    Dim vicePres() As String = IO.File.ReadAllLines("VPres.txt")
    Dim presidents() As String = IO.File.ReadAllLines("USPres.txt")
    Requirement: The program also should display the number of those names in a message box.
    Interpretation: The messagebox should display the total number of names in the file (of persons who have been either President, Vice President, which would include those that have been both).

    I also have to create a third array that names every person that served as both.
    Where did that come from? Is that a requirement? Also, if you did fill that array with your code it wouldn't be every person that served as both, but every person who served as either (if you do a Union), so should be the output you're looking for, for your output file and messagebox, assuming the below statement of work is correct.

    Probably the full statement of work is:
    You are given two files, one lists the names of persons who have served as presidents, the second the names of persons who have served as vice-presidents.
    Write a program that creates a file containing the names of every person who served as either vice-president or president.
    The program also should display the number of those names in a message box.
    Expected Result: The number displayed should be less then the number in the two lists combined as some persons have served as both vice-president and president.

    As for the Union line not working, shouldn't that be
    eitherOR = vicePres.Union(presidents).ToArray
    Last edited by passel; Nov 6th, 2014 at 01:11 PM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help on Union of Arrays

    Quote Originally Posted by Evil_Giraffe View Post
    Quote Originally Posted by Shaggy Hiker View Post
    I second EG's confusion.
    Quote Originally Posted by techgnome View Post
    Third.... in favor.... Aye!

    -tg
    Van Damme! I obviously did read what I thought I read the first time but, when the OP claimed otherwise, I went back to read it again and stopped after this part:
    Quote Originally Posted by JPA21 View Post
    I also have to create a third array that names every person that served as both. That is where I am having the problem. The rest of it I have figured out.
    When the OP refuted that, I went back and read the post again but stopped after this part:
    Quote Originally Posted by JPA21 View Post
    Write a program that creates a file containing the names of every person who served as either vice-president or president.
    and assumed that I had indeed misread it the first time. I have to stop posting in a hurry.

    So, I was right in the first place. If you want a list of those who served as either then you want a union and if you want a list of those who served as both then you want an intersection. Like I said, primary school maths.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width