http://qs321.pair.com?node_id=1079635


in reply to compare 2 arrays for intersect diff and commmon values

It looks to me that this is not doing what you think:
my %count = (); foreach my $device (@current_list, @temp_devicelist) { $count{$device}++; }
as those arrays are in fact arrays of arrays. Try getting rid of that and putting my %count at the top of your sub and counting within the other loops:
my %count; # ... for ($n=0; $n < $size; $n++) { our $device=$current_list[$n][0]; $count{$device}++; } # ... for ($n=0; $n < $size; $n++) { our $tempdevicelinks=$temp_list[$n][0]; $count{$tempdevicelinks}++; } # ... my @difference = grep { $count{$_} == 1 } keys %count; my @intersect = grep { $count{$_} == 2 } keys %count; my @union = keys %count;
Then you can print out the values:
print "Difference:\n"; print "$_\n" for @difference; print "Intersect:\n"; print "$_\n" for @intersect; print "Union:\n"; print "$_\n" for @union;

Replies are listed 'Best First'.
Re^2: compare 2 arrays for intersect diff and commmon values
by rasgolla (Initiate) on Mar 25, 2014 at 05:53 UTC
    Thanks Tanget !! I have tried changing my %count = (); to my %count; and loop part i didnot understand. I thought $device is somthing which we are looking in both the arrays or it's just any x value ?
    DEBUG( "DEBUG Difference:\n"); DEBUG( "DEBUG: - $_\n for @difference" ); DEBUG( "DEBUG: - Intersect:\n "); DEBUG( "DEBUG: - $_\n for @intersect )"; DEBUG( "DEBUG: - Union:\n "); DEBUG( "DEBUG: - $_\n for @union " );
    I am still getting the same error ...
      Where you have our $device=$current_list[$n][0] the device name is now in $device, so that is what you want to count. Right after that add the line $count{$device}++;. Then do the same for where you have our $tempdevicelinks=$temp_list[$n][0] - add the line $count{$tempdevicelinks}++;. You could just copy and paste my sections of code into the relevant parts of your own code, and delete from your own code this bit:
      foreach my $device (@current_list, @temp_devicelist) { $count{$device}++; }

        Tangent Unfortuenetly there is no output. Also now I am unable to get device name now. I am pasting changed code again.I have tried putting debug statement as well but no output.Please let me know if it is correct. I a really thankful to you that you are giiving your preciosu time to me.

        sub updatedevice() { my $n = {}; my %count; my $Device_LINK = $server->object("ICF_PersistentDataSet",$devicelinks +); my $Temp_Device_LINK = $server->object("ICF_PersistentDataSet",$tempde +vicelinks); my @current_devicelist = @{ $Device_LINK->invoke("get") }; my @temp_devicelist = @{ $Temp_Device_LINK->invoke("get") }; my %temp_list; my %current_list; my $size = @current_list; for ($n=0; $n < $size; $n++) { our $device=$current_list[$n][0]; $count{$device}++; } DEBUG( "DEBUG: - devicelinks values $device " ); my $size = @temp_list; for ($n=0; $n < $size; $n++) { our $tempdevicelinks=$temp_list[$n][0]; $count{$tempdevicelinks}++; } DEBUG( "DEBUG: - temp plc links values $tempdevicelinks " ); my @difference = grep { $count{$_} == 1 } keys %count; my @intersect = grep { $count{$_} == 2 } keys %count; my @union = keys %count; #DEBUG( "DEBUG Difference:\n"); #DEBUG( "DEBUG: - $_\n for @difference" ); #DEBUG( "DEBUG: - Intersect:\n "); #DEBUG( "DEBUG: - $_\n for @intersect "); #DEBUG( "DEBUG: - Union:\n "); #DEBUG( "DEBUG: - $_\n for @union " ); print "Difference:\n"; print "$_\n" for @difference; print "Intersect:\n"; print "$_\n" for @intersect; print "Union:\n"; print "$_\n" for @union;

        You are genious !!!! Thank you sooo Muchh Tanget ...My issue resolved... Thanks for yout time...