Re: find words not in an array

From: mbstevens (NOXwebmasterx_at_xmbstevensx.com)
Date: 03/28/05

  • Next message: Joe Smith: "Re: find words not in an array"
    Date: Mon, 28 Mar 2005 07:46:14 GMT
    
    

    #!/usr/local/bin/perl -w
    use strict;
    #-----------------------------------------------
    # Q: 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?
    #--------------------------------------------
    # A: I like a subroutine version for clarity.
    #--------------------------------------------
    use subs qw (is_in_wordlist);
    my @testlist = ( 'a', 'e', 'i', 'o', 'u');
    my @wordlist = ('zot', 'pook', 'e', 'vee', 'u');
    my @newlist = ();
    #-----------------------------------------
    foreach my $t (@testlist) {
            if ( !is_in_wordlist($t) )
                    {push @newlist, $t;}
    }

    foreach my $n (@newlist)
            {print "$n\n";}
    #-------------------------------------------
    sub
    is_in_wordlist {
            my $sought = shift;
            foreach my $w (@wordlist) {
                    if ($sought eq $w)
                            { return 1; } # found
            }
            return 0; # not found
    }


  • Next message: Joe Smith: "Re: find words not in an array"

    Relevant Pages

    • Re: Consecutive Numbers
      ... I was using wrong arrays in last two subs. ... It seems that foreach is main speed killer. ...
      (comp.lang.perl.misc)
    • Re: Bass speaker/cabinet question
      ... close couple to the floor for increased bass efficiency. ... arrays are the SLA and the Omni 10, and both of them are done that way ... Out of all Bill's sub designs, I found one that had the subs placed on ...
      (alt.guitar.bass)
    • Re: create arrays for GD::Graph::lines
      ... with all the "ancode_n" and foreach code, an array of all the yearly ... But I am stuck trying to get it out into n number of arrays that I ... my @ancodes = sort keys %values; ... [sort keys %years], ...
      (perl.beginners)
    • working with 3-dimensional array
      ... I'm trying to read in two file sets of library records, compare a regex that I find in different lines of each set of records, and then copy one whole line over to the other when a match is found. ... I think that I am reading and pushing the arrays onto the other arrays correctly, but when I'm traversing the arrays to do the conditionals, I am getting uninitialized value warnings, which seems like I'm either not traversing correctly OR I'm not really pushing the data onto the arrays properly. ... sub read_records {# read in whole MARC record and push it to @marc ... the foreach $sl loop ...
      (perl.beginners)
    • RE: Emptying several arrays at once
      ... If you read my original email, I said it was a bad idea and also that you ... would need to turn strict off to do anything like that. ... The example below declared the arrays on the fly with strict not in place. ...
      (perl.beginners)