RosettaCodeData/Task/Set-consolidation/PL-I/set-consolidation.pli
2014-01-17 05:34:36 +00:00

58 lines
1.5 KiB
Text

Set: procedure options (main); /* 13 November 2013 */
declare set(20) character (200) varying;
declare e character (1);
declare (i, n) fixed binary;
set = '';
n = 1;
do until (e = ']');
get edit (e) (a(1)); put edit (e) (a(1));
if e = '}' then n = n + 1; /* end of set. */
if e ^= '{' & e ^= ',' & e ^= '}' & e ^= ' ' then
set(n) = set(n) || e; /* Build set */
end;
/* We have read in all sets. */
n = n - 1; /* we have n sets */
/* Display the sets: */
put skip list ('The original sets:');
do i = 1 to n;
call print(i);
end;
/* Look for sets to combine: */
do i = 2 to n;
if length(set(i)) > 0 then
if search(set(1), set(i)) > 0 then
/* there's at least one common element */
do; call combine (1, i); set(i) = ''; end;
end;
put skip (2) list ('Results:');
do i = 1 to n;
if length(set(i)) > 0 then call print (i);
end;
combine: procedure (p, q);
declare (p, q) fixed binary;
declare e character (1);
declare i fixed binary;
do i = 1 to length(set(q));
e = substr(set(q), i, 1);
if index(set(p), e) = 0 then set(p) = set(p) || e;
end;
end combine;
print: procedure(k);
declare k fixed binary;
declare i fixed binary;
put edit ('{') (a);
do i = 1 to length(set(k));
put edit (substr(set(k), i, 1)) (a);
if i < length(set(k)) then put edit (',') (a);
end;
put edit ('} ') (a);
end print;
end Set;