Re: find words not in an array

From: Jürgen Exner (jurgenex_at_hotmail.com)
Date: 03/28/05


Date: Mon, 28 Mar 2005 06:18:17 GMT

r wrote:
> if I have 2 arrays, @wordlist and @testlist, how can I create a third
> array that contains the words from @testlist that are not common to
> @wordlist?
> I thought I might use grep but can't figure it out.

No, grep isn't quite the right tool. For questions like that a hash is
usually the datastructure of choice.
In this particular case you may want to start with the "perldoc -q
intersection". This FAQ computes the symmetric difference, so you will have
to modify the answer slightly.
Or you simply grab the proper set module from CPAN.

jue



Relevant Pages

  • Re: Find a line in a text file then print a field
    ... Here is "grep" part of my perl script so far (ommitting the various ... the matching lines in an array. ...
    (perl.beginners)
  • Re: How to sort an array which contains a value several times ?
    ... Jay Savage a e'crit: ... > because we want to iterate over @array and filter duplicates out (you ... > cases $is_new remains 0 and grep filters that element out. ...
    (perl.beginners)
  • Re: Re: How to sort an array which contains a value several times ?
    ... > because we want to iterate over @array and filter duplicates out (you ... > cases $is_new remains 0 and grep filters that element out. ...
    (perl.beginners)
  • Re: Can I Grep multiple lines??
    ... grep does not change array elements. ... That distinction gets blurry when the test function in a filter has ... from the original array, but the corresponding elements keep the SAME ...
    (comp.lang.perl.misc)
  • Re: Re: Grep uniqueness issue
    ... Is actually taking each value in @pingErrorsName and validating if it is equal to $element. ... >> I am having an odd problem using grep to ensure an array only contains ... > You should probably use a hash (along with your array, ... That's because \b matches word boundaries. ...
    (perl.beginners)