smoss has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, This is probably a very basic question. I have an array (@a) that contains elements that are strings with text and numbers. I have another array (@b) containing just numbers. I want to search through each element in @a and if the element has any of the numbers from @b in it I want to push the element into a third array. I've tried using regular expressions with loops and I've tried grep but cannot get it to work. Any suggestions?

Replies are listed 'Best First'.
Re: comparing element in arrays
by Cyrnus (Monk) on May 20, 2002 at 08:39 UTC
    Something like this should work
    foreach $element (@a) { foreach $number (@b) { if ($element =~ /\D$number\D/) { push (@c, $element); } } }
    EDIT:added \D around the variable in the REGEX after smoss's response. That should take care of the extra elements your were getting. The way it was before it would pick out 96 in the string if one of the numbers was either 9 or 6, adding the non number boundary should force it to match numbers exactly. It works on my tests. If it still doesn't work for you give me your sample data and I'll see if I can recreate the problem.

    John
      John, Thanks for the response, this doesn't seem to work, it pushes the $element into the array @c regardless of whether the $element contains the $number or not.
      Thanks John, That did the trick.
Re: comparing element in arrays
by Zaxo (Archbishop) on May 20, 2002 at 08:35 UTC

    You could try something like this:

    my %numbered; foreach my $string (@a) { for ($string =~ /(\d+)/g) { push @{$numbered{$_}}, $string; } } my @occurances; for (@b) { push @occurances, @{$numbered{$_}} if exists $numbered{$_}; }
    Rough code, untested. Your data may allow some simplification.

    After Compline,
    Zaxo

Re: comparing element in arrays
by gmax (Abbot) on May 20, 2002 at 12:09 UTC
    You can also achieve your purpose with a nested grep.
    #!/usr/bin/perl -w use strict; my @texts = ('this is 110', 'cats have 9 lives', 'dogs have 4 legs', 'no numbers here', 'my uncle has 5 children and 17 grandchildren'); my @numbers = (1, 11, 5, 4, 67, 9, 45); # for efficiency, we turn all numbers into RegExes my @regex_num = map {qr /\D$_\D/} @numbers; my @matches = grep {my $txt=$_; grep {$txt =~ /$_/} @regex_num } @texts; print join "\n", @matches, "\n"; __END__ cats have 9 lives dogs have 4 legs my uncle has 5 children and 17 grandchildren
    Your request is a special case of another interesting one that was answered some weeks ago (comparing arrays). Please have a look at it also for a discussion on efficiency.
     _  _ _  _  
    (_|| | |(_|><
     _|   
    
Re: comparing element in arrays
by rob_au (Abbot) on May 20, 2002 at 11:29 UTC
    How about this snippet of code which seems to do what you need ...

    my @a = ( 'a1', 'b2', 'c3', 'd4' ); my @b = ( '3', '4', '5', '6' ); my @results; foreach my $num (@b) { push @results, grep { /$num/ } @a; } # @results will contain 'c3' and 'd4'

    Update - Erk, modified answer to address the question at hand - If in fact you do simply need to find the union of two arrays, this node should help you out :-)

     

Re: comparing element in arrays
by particle (Vicar) on May 20, 2002 at 11:55 UTC
    some good answers have been given here. i'd like to add that basic questions can often be answered by a visit to Super Search (as long as the search terms are 4+ character words ): google's advanced search should also provide some assistance.

    ~Particle *accelerates*