compare/match arrays

Hi there all,

I am having a question.
Is it posible to compare elements of 2 different arrays?

For example I got
Array 1 | Array 2
123_abc | 123_bcd
123_bcd | 234_bcd
234_abc | 567_abc
234_bcd | 123_abc

than the match is
123_abc & 234_bcd and non of the others.
So how can I get this done?
to compare it?
yea with if statement but dont know how to get the compare on a whole aray.
I get if [ ${array1[0]} = ${array2[0]} ]
But that isnt what I need.
more like wild card all of array 2 to see if the words are a match.

Sorry I always sound so chaotic but I hope you people understand.
Thanx in advance!!!

You didn't say what shell you were using so I'll presume ksh.

You can compare the entire arrays using if [[ "${array1[@]}" = "${array2[@]}" ]] because ${array[@]} expands to the entire contents of an array.

Obviously if you wanted to know which element differed you would need to go through it one-by-one.

Hi,

below perl script may help you, but if the arrays are too big, i do not think it is a good way to sort. May need more consideration on compare logic.

@arr=('aa','bb','cc');
@brr=('bb','dd','ee');
print "Match: $_" foreach(grep {mat($_);} @arr);
sub mat{
	$t=shift;
	foreach(@brr){
		return $_ if ($_ eq $t);
	}
}
Match:bb

You say you want to compare arrays but we need an idea on what you want to compare. Usually when comparing you're comparing with a pattern. i.e. does pattern 1 match pattern 2. Maybe you're wanting to combine the array elements after matching them with a pattern, and if so, what would be the pattern or rule you would use to compare the elements? After we know that then we can construct a routine that can combine them together.