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

Hi, I have to compare 2 string arrays. my 2 arrays are as follows:
array1 array2 abc_RCF_BM_allocation_0_kl RCF_BM_allocation_0_in aplc_RCF_BM_allocation_0_in_nml BM_allocation_0 fbe_RCF_BM_allocation_0_in_tvg RCF_BM_allocation_0 ap_hu_BM_allocation_0_in_nml
I have to check whether array1 contain array2 or not. therefor my ouput should be like:
abc_RCF_BM_allocation_0_kl contains RCF_BM_allocation_0 aplc_RCF_BM_allocation_0_in_nml contains RCF_BM_allocation_0_in fbe_RCF_BM_allocation_0_in_tvg contains RCF_BM_allocation_0_in ap_hu_BM_allocation_0_in_nml contains BM_allocation_0
I tried with:
for($i=0;$i<$array1length;$i++){ for($j=0;$j<$array2length;$j++){ if($array1[$i] =~ /\b$array2[$j]\b/) {print "$array1[$i] contains $array2[$j]"; }}}
But it didnt work. Please guide me to compare the strict strings.
Thanking you,

Replies are listed 'Best First'.
Re: check 2 string arrays
by toolic (Bishop) on Mar 31, 2009 at 13:01 UTC
    The short answer is that you should not use the \b anchors in this case; just remove them from your regex.

    Here is a more Perl-ish way to code this up:

    use strict; use warnings; my @array1 = qw( abc_RCF_BM_allocation_0_kl aplc_RCF_BM_allocation_0_in_nml fbe_RCF_BM_allocation_0_in_tvg ap_hu_BM_allocation_0_in_nml ); my @array2 = qw( RCF_BM_allocation_0_in BM_allocation_0 RCF_BM_allocation_0 ); for my $i (0 .. $#array1){ for my $j (0 .. $#array2){ if ($array1[$i] =~ /$array2[$j]/) { print "$array1[$i] contains $array2[$j]\n"; last; } } } __END__ abc_RCF_BM_allocation_0_kl contains BM_allocation_0 aplc_RCF_BM_allocation_0_in_nml contains RCF_BM_allocation_0_in fbe_RCF_BM_allocation_0_in_tvg contains RCF_BM_allocation_0_in ap_hu_BM_allocation_0_in_nml contains BM_allocation_0
      Hi Toolic,
      I tried with if ($array1[$i] =~ /$array2[$j]/) But i still have some error.e.g. it shows 2 outputs for some strings
      abc_RCF_BM_allocation_0_kl contains BM_allocation_0 abc_RCF_BM_allocation_0_kl contains RCF_BM_allocation_0 aplc_RCF_BM_allocation_0_in_nml contains RCF_BM_allocation_0_in aplc_RCF_BM_allocation_0_in_nml contains BM_allocation_0
      Still i didnt get what could be the problem.

        Note the 'last;' in toolics version of the search loop. That makes sure that the loop stops as soon as the first solution is found.

        After that the only thing left to do is to order array2 so that the first found solution is always the one you want

Re: check 2 string arrays
by eff_i_g (Curate) on Mar 31, 2009 at 14:11 UTC
    You've posted about this numerous times, each being slightly different. I still recommend List::Compare.
Re: check 2 string arrays
by jethro (Monsignor) on Mar 31, 2009 at 13:08 UTC

    The long answer is that \b checks for the boundary of words and non-words characters (\w and \W). You seem to think that the underscore '_' belongs into the non-word category, but for perl it counts as a word character.

Re: check 2 string arrays
by cdarke (Prior) on Mar 31, 2009 at 13:13 UTC
    _ is not part of the \b character class. TMTOWTDI:
    use strict; use warnings; my @array1 = qw (abc_RCF_BM_allocation_0_kl aplc_RCF_BM_allocation_0_in_nml fbe_RCF_BM_allocation_0_in_tvg ap_hu_BM_allocation_0_in_nml); my @array2 = qw(RCF_BM_allocation_0_in BM_allocation_0 RCF_BM_allocation_0); for my $pattern (@array2) { my @results = grep /$pattern/, @array1; print "$pattern is in @results\n"; }
    Update: This detects BM_allocation_0 even when it is part of RCF_BM_allocation_0, but it is unclear what is required here.
      Hi cdarke, I tried with
      for my $pattern (@array2) { my @results = grep /$pattern/, @array1; print "$pattern is in @results\n"; }
      But i still have some error.e.g. it shows 2 outputs for some strings
      abc_RCF_BM_allocation_0_kl contains BM_allocation_0 abc_RCF_BM_allocation_0_kl contains RCF_BM_allocation_0 aplc_RCF_BM_allocation_0_in_nml contains RCF_BM_allocation_0_in aplc_RCF_BM_allocation_0_in_nml contains BM_allocation_0
      Still i didnt get what could be the problem.
Re: check 2 string arrays
by Ish (Acolyte) on Mar 31, 2009 at 16:17 UTC
    Hi sharan, Did you actually run the code toolic provided? I did and it does appear to produce the output as you wanted it, based on the supplied data. If this is not what you wanted possibly you should expand or rephrase your question.