Jump to content

Common value in three arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
terrylau

terrylau

    Newbie

  • Members
  • Pip
  • 6 posts
Perl newbie here… need some help and suggestions. I’ve searched for many examples but so far it’s all for array operation for two arrays. I have three arrays and want to find the common value in the three array and list them out :

Array 1 = {0, 1,2,3,4,5,6,7,8,9}
Array 2 = {1,2,3,4,6,8, 10, 12,14}
Array 3 = {1,2,3,5,7,9, 11,13,15}

I want the output to be something like below:
 
             Array 1            Array 2            Array 3
  0          yes                   
  1          yes                   yes                   yes
  2          yes                   yes                   yes
  3          yes                   yes                   yes
  4          yes                   yes                   
  5          yes                                           yes
  6          yes                   yes                   
  7          yes                                           yes
  8          yes                   yes                   
  9          yes                                           yes
  10                                yes                   
  11                                                        yes
  12                                yes                   
  13                                                        yes
  14                                yes                   
  15                                                        yes
   
The idea is to list out the output as above. I’ve started with something below which I got from another website but got stuck :
 
  #!/usr/bin/perl
  use strict;
use warnings;
  my @array1;
my @array2;
my @diff;
my @isect;
my $item;
my %count;
  @array 1 = (0, 1,2,3,4,5,6,7,8,9);
  @array 2 = (1,2,3,4,6,8, 10, 12,14);
  @isect = ( );
@diff = ( );
%count = ( );
  foreach $item (@array1, @array2) { $count{$item}++;}
  foreach $item (keys %count) {
if ($count{$item} == 2) {
push @isect, $item;
} else {
push @diff, $item;
}
}
  print "\nA Array = @array1\n";
  print "\nB Array = @array2\n";
  print "\nIntersect Array = @isect\n";
  print "\nDiff Array = @diff\n\n";
   
Appreciate the help

#2
johnny123

johnny123

    Newbie

  • Members
  • PipPip
  • 10 posts
Did you ever get this to work?

#3
shawnhcorey

shawnhcorey

    Newbie

  • Members
  • Pip
  • 2 posts
You may want to tweak the output.

#!/usr/bin/perl


use strict;

use warnings;


use Data::Dumper;


# Make Data::Dumper pretty

$Data::Dumper::Sortkeys = 1;

$Data::Dumper::Indent   = 1;


# Set maximum depth for Data::Dumper, zero means unlimited

$Data::Dumper::Maxdepth = 0;


my @array1 = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );

my @array2 = ( 1, 2, 3, 4, 6, 8, 10, 12, 14 );

my @array3 = ( 1, 2, 3, 5, 7, 9, 11, 13, 15 );


my %all = ();


for my $item ( @array1 ){

  $all{''}{$item} ++;

  $all{array1}{$item} ++;

}


for my $item ( @array2 ){

  $all{''}{$item} ++;

  $all{array2}{$item} ++;

}


for my $item ( @array3 ){

  $all{''}{$item} ++;

  $all{array3}{$item} ++;

}


# print 'all ', Dumper \%all;  # uncomment for debugging


printf "%20s %20s %20s %20s\n", qw( Item Array1 Array2 Array3 );


for my $item ( sort { $a <=> $b } keys %{ $all{''} } ){

  my @list = ( $item );


  if( exists $all{array1}{$item} ){

    push @list, 'yes';

  }else{

    push @list, '';

  }


  if( exists $all{array2}{$item} ){

    push @list, 'yes';

  }else{

    push @list, '';

  }


  if( exists $all{array3}{$item} ){

    push @list, 'yes';

  }else{

    push @list, '';

  }


  printf "%20s %20s %20s %20s\n", @list;

}



#4
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
I like to break things like this apart into chunks that are easy to handle and understand. This is how I would go about it without advanced knowledge of perl since I have only a basic understanding of the language myself.
#!/usr/bin/perl

my @array1 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
my @array2 = (1, 2, 3, 4, 6, 8, 10, 12, 14);
my @array3 = (1, 2, 3, 5, 7, 9, 11, 13, 15);
my @list = @array1;

sub checkIfElementInArray{
    my ($element, @array) = @_;
    for(my $j=0; $j<=$#array; $j++){
        if($array[$j]==$element){
            return 1;
        }
    }
    return 0;
}

sub buildList{
    my ($arrayRef1, $arrayRef2) = @_;
    my @list = @{$arrayRef1};
    my @array2 = @{$arrayRef2};
    for(my $i=0; $i<=$#array2; $i++){
        if(!checkIfElementInArray($array2[$i],@list)){
            $list[$#list+1]=$array2[$i];
        }
    }
    return @list;
}

sub sortList{
    my (@list) = @_;
    my @sorted_numbers = sort {$a <=> $b} @list;
    return @sorted_numbers;
}

sub printLine{
    my ($string1, $string2, $string3, $string4) = @_;
    print "$string1\t\t$string2\t\t$string3\t\t$string4\n";
}

sub checkElementAndPrint{
    my ($element,$array1Ref,$array2Ref,$array3Ref) = @_;
    my ($a1,$a2,$a3) = ("","","");
    if(checkIfElementInArray($element,@{$array1Ref})){
        $a1="yes";
    }
    if(checkIfElementInArray($element,@{$array2Ref})){
        $a2="yes";
    }
    if(checkIfElementInArray($element,@{$array3Ref})){
        $a3="yes";
    }
    printLine($element,$a1,$a2,$a3);
}

sub createTable{
    my ($listRef,$array1Ref,$array2Ref,$array3Ref) = @_;
    printLine("","Array 1","Array 2","Array 3");
    my @list = @{$listRef};
    my $size = $#list;
    for(my $i=0; $i<=$size; $i++){
        checkElementAndPrint($list[$i],$array1Ref,$array2Ref,$array3Ref);
    }
}

@list = buildList(\@list,\@array2);
@list = buildList(\@list,\@array3);
@list = sortList(@list);
createTable(\@list,\@array1,\@array2,\@array3);

twas brillig