Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

compare 2 arrays for intersect diff and commmon values

by rasgolla (Initiate)
on Mar 25, 2014 at 04:18 UTC ( #1079630=perlquestion: print w/replies, xml ) Need Help??

rasgolla has asked for the wisdom of the Perl Monks concerning the following question:

Hi Team, I am getting output as difference links values ARRAY(0x34276a0) intersect links values union links values ARRAY(0x34276a0). Please please help me why and where to do changes to make correct values appeared on difference ,intersection and union. Actaully after finding the difference I want to remove it from dataset ..Firstly I have tried a lot to find correct answers on different sites and finally landed here...please helppp meee..

sub updatedevice() { my $n = {}; 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]; DEBUG( "DEBUG: - devicelinks values $device " ); --- > abl +e to print this value of device "ABC-DCFE41->90" my $size = @temp_list; for ($n=0; $n < $size; $n++) { our $tempdevicelinks=$temp_list[$n][0]; DEBUG( "DEBUG: - temp plc links values $tempdevicelinks " ); --- > abl +e to print this value of device "GHJKL-poiu->78" my %count = (); foreach my $device (@current_list, @temp_devicelist) { $count{$device}++; } my @difference = grep { $count{$_} == 1 } keys %count; my @intersect = grep { $count{$_} == 2 } keys %count; my @union = keys %count; DEBUG( "DEBUG: - difference links values $difference[0] " ); DEBUG( "DEBUG: - intersect links values $intersect[0] " ); DEBUG( "DEBUG: - union links values $union[0] " ); } } }

Replies are listed 'Best First'.
Re: compare 2 arrays for intersect diff and commmon values
by kcott (Archbishop) on Mar 25, 2014 at 04:43 UTC

    G'day rasgolla,

    Welcome to the monastery.

    "I am getting output as ..."

    Please show actual output between <code>...</code> tags. That way we can see exactly what you're seeing.

    Please also show expected output (again, between <code>...</code> tags). Then we can see the difference between what you got and what you were trying to get.

    From what you've posted, if

    DEBUG( "DEBUG: - difference links values $difference[0] " );

    is producing

    DEBUG: - difference links values ARRAY(0x34276a0)

    but you actually wanted something like

    DEBUG: - difference links values val1 val2 ... valN

    then you'll need to dereference the $difference[0] arrayref:

    DEBUG( "DEBUG: - difference links values @{$difference[0]} " );

    That possibly raises a few questions, most of which may be answered by "Perl references short introduction".

    If you have further questions, please follow the guidelines in "How do I post a question effectively?". Also, using a logical indentation, will make your code more readable for both you and us: that equates to less errors by you and, for us, a greater inclination to actually read your code.

    -- Ken

      Thanks for the response Ken. I have tried 3 ways but all of them throwing error:

      1). As suggested:

      @{$difference[0]}
      Error -- >Can't use string ("ARRAY(0x2c992e0)") as an ARRAY ref while +"strict refs" in use at

      2). Tried 2nd way

      @{$difference}
      Error as below -- >
      Global symbol "$difference" requires explicit package name at Global symbol "$intersect" requires explicit package name at Global symbol "$union" requires explicit package name at .

      3). tried 3rd way

      @difference
      DEBUG: - difference links values ARRAY(0x32690e0) ARRAY(0x3269090) ARR +AY(0x322d040) ARRAY(0x3276990) ARRAY(0x322d060) ARRAY(0x3269f80) ARRA +Y(0x3269040) DEBUG: - intersect links values DEBUG: - union links values ARRAY(0x32690e0) ARRAY(0x3269090) ARRAY(0x +322d040) ARRAY(0x3276990) ARRAY(0x322d060) ARRAY(0x3269f80) ARRAY(0x3 +269040)

      Expected output should be device names(e.g ..ABC-DCFE41->90) so that I can remove difference part one by one from the data set.

Re: compare 2 arrays for intersect diff and commmon values
by tangent (Vicar) on Mar 25, 2014 at 04:56 UTC
    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;
      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}++; }
Re: compare 2 arrays for intersect diff and commmon values
by Bloodnok (Vicar) on Mar 25, 2014 at 09:45 UTC
    For array difference, union & intersection, I would refer you to Recipe 4.9 in the Perl Cookbook.

    A user level that continues to overstate my experience :-))
Re: compare 2 arrays for intersect diff and commmon values
by hippo (Bishop) on Mar 25, 2014 at 10:24 UTC

    Have you read the FAQ? It's a great place to start.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1079630]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-06-27 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (88 votes). Check out past polls.

    Notices?