in reply to compare 2 arrays of strings

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.

Replies are listed 'Best First'.
Re^2: compare 2 arrays of strings
by almut (Canon) on Mar 06, 2009 at 15:33 UTC
    $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>

Re^2: compare 2 arrays of strings
by sharan (Acolyte) on Mar 06, 2009 at 14:54 UTC
    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.