in one array but not in the other

Scott K. Laws scott at elvis.mu.org
Wed May 17 15:28:57 CDT 2000


This is probbly not teh best way but ......

$ perl -e '                                  
> @a = (1,2,3,4,5,6,7,8,9); @b = (2,4,6,8);
> foreach $a (@a) {                                       
>   if(!grep(/$a/, at b)) {
>     push(@l,$a);
>   }
> }
> print join(" ", at l) . "\n";
> '
1 3 5 7 9

also somewhat less redable is:


$ perl -e '
  @a = (1,2,3,4,5,6,7,8,9); @b = (2,4,6,8);
 @list = grep { $a = $_; !grep { /$a/ } @b } @a;
print join(" ", at list) . "\n";
'
1 3 5 7 9


Regards,
Scott
On Thu, 18 May 2000, Jay Buffington wrote:

> I have two arrays (each has a lot of elements which are strings).  I
> want to know which items are in array1 but not in array2.
> 
> This is the best I could come up with but there is bound to be a better
> way:
> 
> foreach $element (@array1) { $count{$element} += 2; }
> foreach $element (@array2) { $count{$element}++; }
> foreach $element (keys %count) { push @array3, $element if
> ($count{$element} == 1); }
> 
> Is there a better way to accomplish this?
> 
> Thanks
> Jay
> 




More information about the Columbia-pm mailing list