Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Suppose I have two arrays of positive integers, @one and @two, potentially containing several thousand elements.

I want to compute the array @inter which contains those elements which are in both @one and @two, and the array @union which contains those elements which are in either @one or @two.

Here's my first cut at doing this:

intersection: Pick one of the arrays, put the other in a hash with duumy values, and loop through the first array, adding the elements which are also in the other array to @inter.

Code
code:

union: Put both arrays in the same hash with dummy values, so duplicates are ignored, then extract the keys.

Code
code:

Does anyone know a better way of doing this?

Sponsored Links
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
IIRC, there is an example of an intersection in the perl documentation

perldoc -q intersection

I'm also positive I've seen Union and Intersection stuff in the Mastering Algorithms with Perl book, which I don't happen to have accesable at the moment, or I would dig them out for you.

You may wanna check the O'Reilly ftp site and see if there are any source code examples from the Algorithm book there. Perhaps the union/intersection code is given in there.

As for my own methods, I don't often do those, so don't have anything to offer without constructing something. when I've had to do it with hashes though, i've used hash slices.

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Thanks, Mark smile

I had searched perldoc for "set", but didn't think to look for "intersection".

"perl -q intersection" does include a method for computing the intersection, as well as the union and symmetric difference, and looks nicer than my approach. It also looks like it generalizes easily to more than two sets.

Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Very cool! Glad the info helped smile

(btw, im interested to see if you get some cool 1 liner replies in response to your usenet post in c.l.p.moderated too ;))

[edit looks like you got a reply about using a hash slice :)]

--mark

[ April 18, 2001 12:24 AM: Message edited by: Mark Badolato ]


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan

Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Bill B
Bill B
Issaquah, WA
Posts: 87
Joined: December 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20240506)