Intersection and union of array by hash


 
Thread Tools Search this Thread
Top Forums Web Development Intersection and union of array by hash
# 1  
Old 02-28-2011
Intersection and union of array by hash

Hi,

A piece of script from Perl-cookbook I do not understand, and post here for explanation.
The purpose is to find the element in either array (union), and in both array (intersection). Thank you in advance.

Code:
@a=qw(1 3 5 6 7 8);
@b=qw(2 3 5 7 9);

foreach $e (@a, @b) {$union{$e}++ && $isect{$e}++}

@union = keys %union;
@isect = keys %isect;

How is the third line:
Code:
foreach $e (@a, @b) {$union{$e}++ && $isect{$e}++}

working behind? I am desperate to understand it.
# 2  
Old 02-28-2011
We loop through the elements of these two arrays:
Code:
@a=qw(1 3 5 6 7 8);
@b=qw(2 3 5 7 9);

The first time a value is seen, its value in the hash union is 0, so it evaluates to false. The second part of the expression $union{$e}++ && $isect{$e}++, the post-increment assignment of the hash isect is not evaluated:

In the first iteration we have:

1. $e gets the value of 1.
2. $union{ $e }++ evaluates to false, $union{ $e } becomes 1 after the expression is evaluated (because it's post-incremented, ++$union{ $e } is the opposite, it will first assign the value of 1 to the variable and after that, the expression will get evaluated, using the new value).
3. $isect{ $e }++ is skipped, because the first part of the expression is false.

Second iteration:

1. $e gets the value of 3 (the second element in the first array).
2 to 3 are the same as in the first case.

...

This way, we build the union of the two sets.


8th iteration:

1. $e gets the value of 3 (the second element in the second array).
2. $union{ $e }++ this time evaluates to true, it's current value is 1, the next one will be 2.
3. $isect{ $e }++ gets evaluated this time, because the first evaluates to true.
So this hash will contain only the values previously seen (those present in the first array (assuming each array contains only unique values).

So now we are able to build the intersection of those two sets.


HTH
This User Gave Thanks to radoulov For This Post:
# 3  
Old 02-28-2011
Re: Intersection and union of array by hash

Thanks!
I got it now, but it seems to me if there is duplicate items in either of the arrays, it won't work, right?

Thank you again!

YT
# 4  
Old 02-28-2011
Quote:
Originally Posted by yifangt
Thanks!
I got it now, but it seems to me if there is duplicate items in either of the arrays, it won't work, right?
Correct. That's why I said "assuming each array contains only unique values".

The union will work anyway (it's a union, not union all).
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Event driven programming / epoll / typedef union / session data array

Sorry for the “word salad” subject, but I wanted to cast a wide net for help. I've created an IP (Internet Protocol) server which serves HTTP, SMTP, and FTP requests. As you probably know, they all require creating a socket, listening on it, accepting connections, and then having a short... (3 Replies)
Discussion started by: John S.
3 Replies

2. Programming

Perl Array within an hash

Hi All I have been using a curl code to output an hash that looks like this $VAR1 = { 'data'... (5 Replies)
Discussion started by: ab52
5 Replies

3. Shell Programming and Scripting

array of hash - perl

How do I get the unique hashes from an array of hashes? @ar1 = ( {a=>1,b=>2}, {c=>3,d=>4},{a=>1,b=>2});I need : @ar2 = ( {a=>1,b=>2}, {c=>3,d=>4});Thanks. (2 Replies)
Discussion started by: shellwell
2 Replies

4. Shell Programming and Scripting

perl Can't coerce array into hash at

Hi guys I have this part of a perl script that returns and odd error if ($args{software}) { print " @DISTFILE_GROUPS $output->{distfile_groups}->{ get_rdist_groups}\n"; and the error is Can't coerce array into hash at i've never seed this error before, any ideas thanks... (0 Replies)
Discussion started by: ab52
0 Replies

5. Shell Programming and Scripting

perl - need help with 2 arrays to hash or 2d array?

I have 2 arrays: @array1 outputs the following: 1 1 1 2 @array2 outputs the following A B C D (2 Replies)
Discussion started by: streetfighter2
2 Replies

6. Shell Programming and Scripting

Array of hash in perl does not work

Hi , I have an input.txt file that i read node: id= c1, class=nb, cx=100, cy=100, r=10 node: id=c2, class=b, cx=150, cy=130, r=10 node: id=c3, class=nb, cx=50, cy=80, r=10 node: id=c4, class=nb, cx=120, cy=200, r=10 i split over , and = to create a global array and then passed it to a... (6 Replies)
Discussion started by: rsanjay
6 Replies

7. Shell Programming and Scripting

Read csv into Hash array?

Hi all experts, May I know how to read a csv file and read the content in a hash in PERL? Currently, I hard-coded and defined it in my code. I wanna know how to make up the %mymap hash thru reading the cfg.txt ==== csv file(cfg.txt): 888,444 999,333 === #!/usr/bin/perl my... (1 Reply)
Discussion started by: kinmak
1 Replies

8. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

9. Shell Programming and Scripting

hash,array and perl

Hi,i have a code fragment below. %tag = (); #line 1 $tag{'info'} = $datastring; #line 2 $resp = $ua->request( #$ua is a user agent POST 'http://10.2.3.0' , Content_Type => application/x-www-form-urlencoded Content => #line 3 I am not sure of what the code... (3 Replies)
Discussion started by: new2ss
3 Replies

10. Shell Programming and Scripting

Hash within array, within hash, within array...

I have a little problem. To keep a configuration simple, I've exceeded my perl knowledge. :-) I've worked with multi-dimentional arrays before, but this one has me beat: @info = ( { 'defval' => 'abc' 'stats' = ( { 'name' => 'a', }, { 'name' =>... (1 Reply)
Discussion started by: jsmoriss
1 Replies
Login or Register to Ask a Question