This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector Question

 
Aravind Ramanujam
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have 2 vectors, vector1 has objects of person and vector2 has objects of company.

A new Vector should be created merging these 2 vectors. The object in the new vector has all the attributes from both Person and Company.


The constraint is, if the Id matches all the fields in Member object will be populated. If the id doesn't match, only the corresponding fields of the Member object will be populated.

Is there a easy way to compare each element of the 2 vectors? I tried a simple for loop iterating through the elements but its not working out right.

Any help is appreciated.
Thanks
A.R
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aravind:

The constraint is, if the Id matches


Whose id are you matching with who's ?

Assuming Member object's id with the id of Person and Company objetct's, then what does the following statement mean?

If the id doesn't match, only the corresponding fields of the Member object will be populated.


Can you please elaborate?
 
Aravind Ramanujam
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i wasn't clear the first time. Hope the below example will make my question clear:

person1(Id:1, Name:John, Addr:3 Main st, City:Chicago, State:IL)
person2(Id:2, Name:Mike, Addr:12 6th st, City:San Francisco, State:CA)
person3(Id:3, Name: Diana, Addr:1635 Auburn Bl, City: Portland, State: OR)

comp1(Id:1, Company:XYZ, Position: Programmer)
comp3(Id:3, Company:HTC, Position:Sales Manager)
comp10(Id:10, Company:ABC, Position:Manager)

member1(Id:1, Name:John, Addr:3 Main st, City:Chicago, State:IL, Company:XYZ, Position: Programmer)
member2(Id:2, Name:Mike, Addr:12 6th st, City:San Francisco, State:CA, Company:, Position: )
member3(Id:3, Name: Diana, Addr:1635 Auburn Bl, City: Portland, State: OR, Company:HTC, Position:Sales Manager)
member10(Id:10, Name:, Addr:, City:, State:, Company:ABC, Position:Manager)

Information for Ids 1 & 3 are merged together. ID 2 has only person info and ID 10 has only company info.
[ December 09, 2008: Message edited by: Aravind Ramanujam ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the two vectors guaranteed to be sorted? (in order of ID, ascending)
 
Aravind Ramanujam
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. They are not.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmph. for some reason, I'm not getting the code formatting. There's lots of extraneous code in here, but it's something you can copy/paste into an IDE and start tinkering with.

I was thinking if they were sorted, there would be a single-pass way to do it. But since they're not, this will do.


The output of the above, with my implementation, is:


The final output is not in the form of a Vector, but you could either post-process it into one, or use a Vector to start with, and instead of a key-value "mapping" in the HashMap, you can use the index of the merge vector itself (when adding to it, you'd be using the add method that inserts into a specified index). This implies then, you'd do *pre* processing, to determine how large your merge vector should be made.
[ December 09, 2008: Message edited by: Mike Curwen ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic