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

Hi all, I am trying to compare the strings of 2 arrays. I have 2 arrays named array1 and array2 and their contents are as follows:
array1 array2 the_for_below and_press this_job online_Please #note that we have space at b +egining of online_Please. complete the_form_below #note that we have space at t +he end of the_form_below. and_press submit_will_only #note that we have space at b +egining of and_press.
I tried to compare the 2 arrays. I should be able to get that
array1[0]=array2[2] i.e. the_for_below = complete array1[3]=array2[0] i.e. the_for_below = and_press
I tried with my code:
for ($k=0;$k<$i;$k++) #where $i is the length of array. { for($l=0;$l<$i;$l++){ if(chomp($array1[$k]) eq chomp($array2[l])) { print"\n$array1[$k],$array2[$l]"; } }
But it does not show the desired output instead displays all the elements of array2 with each of the element of array1. Like...
the_for_below and_press the_for_below online_Please the_for_below the_form_below the_for_below submit_will_only this_job and_press this_job online_Please this_job the_form_below this_job submit_will_only ... and so on..
Please guide me through this... Thanking you

Replies are listed 'Best First'.
Re: compare 2 arrays of strings
by johngg (Canon) on Mar 06, 2009 at 15:57 UTC

    As others have pointed out, chomp is not going to do what you want. The reason all elements of each array are being printed out is that the return from chomp will either be 0 if no line terminator was present at the end of the string, or 1 if there was and it was chomped away. In your arrays there are no terminators so the result of each chomp is zero, the if stringifies the two zeros and compares them resulting in equality true and the print statement is executed.

    Perhaps I'm misunderstanding your question (or not reading your data correctly) but it appears to me that once you correct the chomp issue you will never see any output because there are no common elements. Could it be that the first element in @array1 has a typo and should be "the_form_below" rather than "the_for_below"?

    Cheers,

    JohnGG

    Update: s/equality/true/ to make meaning clearer.

    Update 2: Obviously, I can't read. There is an "and_press" string in each array. Correcting the assumed typo in @array1 and running this code

    use strict; use warnings; my @array1 = ( q{ the_form_below}, q{this_job}, q{complete}, q{ and_press}, ); my @array2 = ( q{and_press}, q{ online_Please}, q{the_form_below }, q{submit_will_only}, ); for my $idx1 ( 0 .. $#array1 ) { for my $idx2 ( 0 .. $#array2 ) { if( cleanUp( $array1[ $idx1 ] ) eq cleanUp( $array2[ $idx2 ] ) + ) { print qq{\$array1[ $idx1 ]: >$array1[ $idx1 ]<\n}, qq{Equivalent to\n}, qq{\$array2[ $idx2 ]: >$array2[ $idx2 ]<\n}, q{=} x 40, qq{\n}; } } } sub cleanUp { my $str = shift; $str =~ s{^\s+}{}; $str =~ s{\s+$}{}; return $str; }

    gives the following output.

    $array1[ 0 ]: > the_form_below< Equivalent to $array2[ 2 ]: >the_form_below < ======================================== $array1[ 3 ]: > and_press< Equivalent to $array2[ 0 ]: >and_press< ========================================
Re: compare 2 arrays of strings
by toolic (Bishop) on Mar 06, 2009 at 14:45 UTC
    chomp only removes the newline character from the end of a string. If you want to strip all types of whitespace characters from the start and end of your strings before your comparison, you could:
    $array1[$k] =~ s/^\s+//; $array1[$k] =~ s/\s+$//; $array2[$l] =~ s/^\s+//; $array2[$l] =~ s/\s+$//;

    You would receive more specific help if you posted complete, running code that we could reproduce, along with your exact output and your exact desired output.

      $array1[$k] =~ s/^\s+//; $array1[$k] =~ s/\s+$//; $array2[$l] =~ s/^\s+//; $array2[$l] =~ s/\s+$//;

      Maybe preferably use a little helper routine (or even existing code from a module such as Text::Trim):

      # modify value in place sub trim { $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; } trim($array1[$k]); trim($array2[$l]); # --- or --- # return the trimmed value sub trim { my $s = shift; $s =~ s/^\s+//; $s =~ s/\s+$//; return $s; } $array1[$k] = trim($array1[$k]); $array2[$l] = trim($array2[$l]);

      This has the advantage of documenting what's going on (with no extra effort), and if you need to trim a third or fourth time, the code would eventually get more compact.  Also, should you ever want to modify the regex, you'll only have to do it one place, so you'll not risk suffering from the cut-n-paste syndrome (one characteristic of which is that it's not only tedious, but also easy to overlook one of the several supposed-to-be-identical code fragments scattered about, when making modifications).

      Also, generally try to avoid the variable name $l. With many fonts, it's difficult to discern $l (dollar-ell) from $1 (dollar-one)...
      </nitpick>

      I tried with your code and i removed chomp as well while comparing. But still the output is not coming correctly. Infact, its able to compare the two strings it seems.
Re: compare 2 arrays of strings
by eff_i_g (Curate) on Mar 06, 2009 at 15:09 UTC
    How about this?
    use List::Compare; use Data::Dumper; s/\A\s+|\s+\z//g for @array1, @array2; my $lc = List::Compare->new(\@array1, \@array2); my @i = $lc->get_intersection(); print Data::Dumper->Dump([\@i]);
Re: compare 2 arrays of strings
by Anonymous Monk on Mar 06, 2009 at 14:39 UTC
Re: compare 2 arrays of strings
by locked_user sundialsvc4 (Abbot) on Mar 06, 2009 at 17:22 UTC

    Can you use a Data::Compare-type module?

Re: compare 2 arrays of strings
by Anonymous Monk on Mar 06, 2009 at 15:02 UTC
    if(chomp($array1[$k]) eq chomp($array2[l])) ---------------------------------------^ print"\n$array1[$k],$array2[$l]"; ----------------------------^