tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ How to handle escaped newlines????

@ISA = qw(FileHandle); can this work? ▻

How do I retrieve all the items that occur in one list but not in another ?

In article <4kl6vv$lp3@nuacht.iol.ie>, John Sheehy <jsheehy@trintech.ie> wrote:
>@LIST1 = a, b, c, d, e;
>
>@LIST2 = a,d;
>
>Is there any convenient operation i.e.
>
>@LIST1 convenient operation @LIST2
>
>that will give me b,c,e. Otherwise, is there any clever way of doing
>this other than a brutwe force search ?

Here's an "array difference" function that's both convenient *and*
brute force :-).

    sub diff {
            my($RAp, $RAq) = @_;
            my %q;

            foreach (@$RAq) {
                    $q{$_} = 1;
            }
            grep ! $q{$_}, @$RAp
    }

You pass it two array references, e.g.

    @LIST3 = diff \@LIST1, \@LIST2;

>Are there any ways of performing set operations such as compement,
>intersection etc.

There's a set of `Set' modules in The Module List, which I've not looked
at.  The above is the first (and only, so far) function I've written for
a "set operations on arrays" module, tentatively called Array::Func.
Some day, I shall add union, intersection, and memberof functions (and
perhaps also "setify", to removes duplicates from an array) and make it
publicly available.

Tim.
--
Tim Goodwin   | "continuous witter about large metal wanking
Cambridge, UK | machines" -- Robin Fairbairns, about uk.transport.

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl.misc
Subject: Re: How do I retrieve all the items that occur in one list but not in another ?
Date: 16 Apr 1996 10:11:06 GMT
Organization: Unipalm PIPEX
Message-ID: <4kvrnq$4kh@wave.news.pipex.net>
References: <4kl6vv$lp3@nuacht.iol.ie>

△ comp.lang.perl △

◅ How to handle escaped newlines????

@ISA = qw(FileHandle); can this work? ▻