1

I have stored values in 5 text files. The values in each text file should be considered as an array. I am trying to write a perl program, to read and print the common elements in these 5 arrays.

For Instance

@a1=(1,7,4,5); 

@a2=(1,9,4,5); 

@a3=qw(1,6,4,5 ); 

@a4=qw(1 2 4 5 ); 

@a5=qw(1 2 4 5 ); 

I expect to print

1 4 5 
6
  • metacpan.org/module/Array::Utils Commented Jul 26, 2012 at 8:44
  • just download this module from cpan and read documentation. Commented Jul 26, 2012 at 8:45
  • 5
    There is no question in this post, and you are expected to have tried your best and reached a barrier before you ask for help here. StackOverflow is not a free software agency. If you want advice on how to read the files you must explain what format they have. Commented Jul 26, 2012 at 8:58
  • I am a biology student, i never experienced with programming, i am learning perl myself. Commented Jul 26, 2012 at 9:03
  • read documentation about print function, about open function and <> operator (it is in IO-handling chapter) Commented Jul 26, 2012 at 9:05

2 Answers 2

1

The perlfaq has lots of answer to questions that are frequently asked. Of course it's all a bit of a waste of time and effort if no-one bothers to check there before asking the question again :-)

How do I compute the difference of two arrays? How do I compute the intersection of two arrays?

Use a hash. Here's code to do both and more. It assumes that each element is unique in a given array:

my (@union, @intersection, @difference);
my %count = ();
foreach my $element (@array1, @array2) { $count{$element}++ }
foreach my $element (keys %count) {
    push @union, $element;
    push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
}

You need the intersection of two arrays. And then do it three more times.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't say what format your input files have, but this program will find all digit strings in each file and list the values common to all of them.

The list of input files is expected as command-line arguments.

use strict;
use warnings;

use File::Slurp 'read_file';

my %counts;

for (@ARGV) {
  $counts{$_}++ for map /\d+/g, read_file $_;
}

my @common = grep $counts{$_} == @ARGV, keys %counts;

printf "(%s)\n", join ', ', @common;

output

(4, 1, 5)

6 Comments

@loldop: because I don't have agree with you or do what you tell me to
your comment to his question (with 3 vote up's) doesn't correlate with your answer.
you answer him, only that. as i see there is no information about format in question. you said that and make general-solution answer.
@loldop: since there is a general solution that is likely to work I gave it. I said he shouldn't ask questions like that, not that I shouldn't answer them. How is it a problem for you?
as he said: biologist and learning perl by myself i think, that this is not he want. Explanation about IO-handling is better in this situation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.