http://qs321.pair.com?node_id=966232

Saadat.123 has asked for the wisdom of the Perl Monks concerning the following question:

Okay I am a typical VBA part-time programmer. Use VBA to solve a few problems. I have been using Perl/Java offtimes to find solutions and automation to a few tasks that I do. Now typically I'd use a database (access) and an SQL statement to get my results but I have been challenged not to use SQL. I have a super list of data (A) and a sublist (B). I need to generate a list (C) that is in (A) but not in (B).... what would be the fastest way in perl?
  • Comment on comparing lists and getting an exemtion report

Replies are listed 'Best First'.
Re: comparing lists and getting an exemtion report
by Riales (Hermit) on Apr 20, 2012 at 18:39 UTC

    How big are these lists? If they're not incredibly large, something like this should work:

    # Assuming @list_a and @list_b my %b; $b{$_} = 1 foreach (@list_b); my @list_c; foreach (@list_a) { push @list_c, $_ unless $b{$_}; }
      # Assuming @list_a and @list_b my %b; $b{$_} = 1 foreach (@list_b); my @list_c = grep { not $b{$_} } @list_a;
Re: comparing lists and getting an exemtion report
by Kenosis (Priest) on Apr 20, 2012 at 19:56 UTC

    Although not as nicely readable as Riales's solution, here's another option:

    map{$setB{$_}++}@listB; @listC = grep{!$setB{$_}}@listA;

    Hope this helps!

Re: comparing lists and getting an exemtion report
by eyepopslikeamosquito (Archbishop) on Apr 21, 2012 at 00:37 UTC

    Essentially the same question was asked today in removing a sublist. You might like to check the answers given there and perlfaq4 (arrays section) and Perl Cookbook, recipe 4.8 "Finding elements in one array but not another".

    Re performance, the way to find out is to use the Benchmark module to compare the running times of various solutions.

Re: comparing lists and getting an exemtion report
by BillKSmith (Monsignor) on Apr 21, 2012 at 01:41 UTC
    The CPAN module List::Compare sounds perfect. I would certainly include it in any benchmark study of this problem. List::Compare
Re: comparing lists and getting an exemtion report
by Anonymous Monk on Apr 20, 2012 at 21:37 UTC
    I think that we would need to know a great deal more about the exact nature of your problem, and of your data and of where that data now is, in order to answer it meaningfully.