2014-05-15 2 views
-1

배열 두 개를 묻는 작업이 있습니다. 더 구체적으로 우리는 "결과는 첫 번째 배열에는 있지만 두 번째 배열에는 존재하지 않는 요소를 포함하는 배열입니다."라고 묻습니다.array2에없는 요소를 인쇄하십시오.

내 출력 인 세 번째 배열에 두 배열에 존재하는 값의 공통점이 있어야한다는 뜻입니까?

@list1 = (1, 2, 3, 4, 5, 6, 7, 8, 9); 
@list2 = (3, 5, 7, 11, 13, 15); 

내 출력은 (3, 5, 7)을 할 건가요 예를 들어 나는 두 개의 배열을 가지고?

+1

과제에서 "차이"의 의미에 대해 묻는 것처럼 보입니다. 이것은 펄 관련 질문이 아닙니다. –

+0

@ NathanFellman : 이것은 'A Δ B'의 차이가 아닙니다. A - B' A - B' – Borodin

+0

상관없이, 이것은 질문을 이해하는 질문입니다. 어쩌면 그것은 집합 이론을 이해하는 문제입니다. Perl에서는 문제가되지 않습니다. –

답변

1

결과는 첫 번째 배열에 있지만 두 번째 배열에는없는 요소를 포함하는 배열입니다.

즉, 원하는 출력은 (1, 2, 4, 6, 8, 9)입니다.

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

#!/usr/bin/perl 
use strict; 
use warnings; 
use Array::Utils qw(:all); 
my @a = qw(1 2 3 4 5 6 7 8 9); 
my @b = qw(3 5 7 11 13 15); 
my @minus = array_minus(@a, @b); 
print @minus; 

또한 대한 perlfaq을 확인하십시오

#!/usr/bin/perl 
# your code goes here 
my @array1 = (1, 2, 3, 4, 5, 6, 7, 8, 9) ; 
my @array2 = (3, 5, 7, 11, 13, 15) ; 
my %tmp ; 

# Store all entries of array2 as hashkeys (values are undef) using a hashslice 
@tmp{@array1} = undef ; 

# delete all entries of array1 from hash using another hashslice 
delete @tmp{@array2} ; 
printf "In Array1 but not in Array2 : %s\n" , join(',' , keys %tmp) ; 

Array::Utils 모듈을 사용하십니까?

관련 문제