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< ========================================
In reply to Re: compare 2 arrays of strings
by johngg
in thread compare 2 arrays of strings
by sharan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |