Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

how to ignore spaces, commas or new line of an array when comparing

by noviceuser (Novice)
on Jun 16, 2021 at 08:01 UTC ( #11133910=perlquestion: print w/replies, xml ) Need Help??

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 (Chancellor) 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?


        🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11133910]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-09-29 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?