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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.