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

I am trying to compare two arrays which may have strings/elements separated by newline, spaces or comma, how can i ignore those special characters while comparing

e.g: suppose @array1 has contents like below

abc xyz

and @array2 has contents like below

abc xyz
my $comp = Array::Compare->new; if ($comp->compare(\@array1, \@array2)) { print "array same\n"; } else { print "array not same\n"; }

Replies are listed 'Best First'.
Re: how to ignore spaces, commas or new line of an array when comparing
by hippo (Bishop) on Jun 16, 2021 at 08:33 UTC

    That almost sounds like the use case for the WhiteSpace option. Did you try that?

    If you still have problems, I recommend that you show precisely how your arrays are created. eg:

    my @array1 = ('abc ', 'xyz');

    ... and so on. This removes any ambiguity about the data you actually have.


      in @array1, the contents are being pushed and in @array2, the contents are being read from a file and stored in the @array2

        How does that reply address "show precisely how your arrays are created" (see I know what I mean. Why don't you?)? Perhaps you intended to write something like:

        use strict; use warnings; my $fStr = <<FSTR; abc xyz FSTR open my $inFile, '<', \$fStr; my @array2 = <$inFile>; my @array1 = ('abc ', 'xyz');
        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        Hmmm. Is your real question then "How do I clean whitespace from data read from a file?" or something like that? Or do you really want one array with whitespace and one without?