Ignore spaces in string match

T

Thelma Lubkin

I am trying to compare two strings that must match except for their
spacing.

If I write:

$h1 = 'here i am';
$h2 = 'herei am';

can I do a less clumsy comparison than

($hh2 = $h2) =~ s/\s//x;
if($hh2 =~ /^$h1$/x) ...

--thanks, thelma
 
M

Matt Garrish

bengee said:
Remove spaces from both strings... if they match then they match!

If you looked at the example, you'd have noticed that the OP already figured
that part out. To the OP, elegance is a matter of taste, but if you're just
looking for speed you could use tr instead of the regexes:

(my $copy1 = $h1) =~ tr/ //d;
(my $copy2 = $h2) =~ tr/ //d;

if ($copy1 eq $copy2) {

}

Without modifying the original variables, I don't expect you find anything
nicer.

As a side note, the code you posted doesn't work. For one, you're only
removing a single space from one of the strings.You're also using the /x
modifier in the first instance where you should be using /g and in the
second where it makes no sense at all. Please see the perlre doc page for
more information on regex modifiers and what they do.

Matt
 
T

Tad McClellan

Thelma Lubkin said:
I am trying to compare two strings that must match except for their
spacing.

If I write:

$h1 = 'here i am';
$h2 = 'herei am';

can I do a less clumsy comparison than

($hh2 = $h2) =~ s/\s//x;


( the s///x option is a no-op there, you must have meant s///g instead?)

( that ignores characters other than the space character. Is that
what you intended? if not then: s/ //g, or better: tr/ //d.
)

I don't think so, normalizing the data before the comparison seems
the best approach to me. You would of course need a similarly
de-spaced copy of $h1 too though.

if($hh2 =~ /^$h1$/x) ...


Do you intend for $h1 to contain regex metacharacters that *are* meta?

If not, then what you have there is an equality test disguised as a
pattern match and implemented in an "expensive" way.

if ( $hh2 eq $hh1 ) # more clear AND faster!
 
T

Thelma Lubkin

: If you looked at the example, you'd have noticed that the OP already figured
: that part out. To the OP, elegance is a matter of taste, but if you're just
: looking for speed you could use tr instead of the regexes:

: (my $copy1 = $h1) =~ tr/ //d;
: (my $copy2 = $h2) =~ tr/ //d;
: if ($copy1 eq $copy2) {
: }
Thanks for the tip: I am going to need many repeats.

: As a side note, the code you posted doesn't work. For one, you're only
: removing a single space from one of the strings.You're also using the /x
: modifier in the first instance where you should be using /g and in the
: second where it makes no sense at all. Please see the perlre doc page for
: more information on regex modifiers and what they do.

That first instance was certainly a careless glitch on my
part.

But if the second /x makes no sense, why does this code work
as I want it to? I've read that /x is recommended for making
complex regular expressions more readable, but it seems to
work also for this use ??

--thelma, trying to learn, not argue


#!/usr/bin/perl -w
#use strict;


my $h1 = 'here i am';
my $h2 = 'herei am';
($hh2 = $h2) =~ s/\s//g;
if($hh2 =~ /^$h1$/x)
{ print "I'm ok for: $h1\n"; }
else
{ print "I'm in a mess for $h1\n"; }

$h3 = 'where am I?';
if($hh2 =~ /^$h3$/x)
{ print "I'm ok for: $h3\n"; }
else
{ print "I'm in a mess for: $h3\n"; }


: Matt
 
T

Thelma Lubkin

: : As a side note, the code you posted doesn't work. For one, you're only
: : removing a single space from one of the strings.You're also using the /x
: : modifier in the first instance where you should be using /g and in the
: : second where it makes no sense at all. Please see the perlre doc page for
: : more information on regex modifiers and what they do.

: That first instance was certainly a careless glitch on my
: part.

: But if the second /x makes no sense, why does this code work
: as I want it to? I've read that /x is recommended for making
: complex regular expressions more readable, but it seems to
: work also for this use ??

: --thelma, trying to learn, not argue



########## Sorry: I can't seem to do anything right this morning.
: #!/usr/bin/perl -w
: #use strict;
^ remove

: my $h1 = 'here i am';
: my $h2 = 'herei am';
my $hh2;
^ add
: ($hh2 = $h2) =~ s/\s//g;
: if($hh2 =~ /^$h1$/x)
: { print "I'm ok for: $h1\n"; }
: else
: { print "I'm in a mess for $h1\n"; }

: my $h3 = 'where am I?';
: ^ add

if($hh2 =~ /^$h3$/x)
: { print "I'm ok for: $h3\n"; }
: else
: { print "I'm in a mess for: $h3\n"; }


: : Matt
 
M

Matt Garrish

Thelma Lubkin said:
: As a side note, the code you posted doesn't work. For one, you're only
: removing a single space from one of the strings.You're also using the /x
: modifier in the first instance where you should be using /g and in the
: second where it makes no sense at all. Please see the perlre doc page
for
: more information on regex modifiers and what they do.

That first instance was certainly a careless glitch on my
part.

But if the second /x makes no sense, why does this code work
as I want it to? I've read that /x is recommended for making
complex regular expressions more readable, but it seems to
work also for this use ??

Because the /x modifier is for adding comments to your regex. You have no
comments or whitespace in your regex, so it's pointless to use the modifier.
Using a modifier for no apparent reason tends to indicate that you either
don't fully understand what they do or why they're there. I was just trying
to point out to you that you should review them. Modifiers should only be
used when you need them, otherwise you introduce the possibility of unwanted
bugs when they start "doing" what they're intended to do.

Matt
 
T

Tore Aursand

I am trying to compare two strings that must match except for their
spacing.

If I write:

$h1 = 'here i am';
$h2 = 'herei am';

can I do a less clumsy comparison than

($hh2 = $h2) =~ s/\s//x;
if($hh2 =~ /^$h1$/x) ...

Remove the spaces in both strings, then do the match;

sub compare_strings {
my $s1 = shift;
my $s2 = shift;

$s1 =~ tr/ //d;
$s2 =~ tr/ //d;

return ( $s1 eq $s2 );
}
 
T

Thelma Lubkin

:> if($hh2 =~ /^$h1$/x) ...

: Do you intend for $h1 to contain regex metacharacters that *are* meta?

: If not, then what you have there is an equality test disguised as a
: pattern match and implemented in an "expensive" way.

It was the preceding sentence that really showed me
how hopeless and useless--and silly--my approach was.
I will do the tr and then the eq comparision.

Thanks to everyone who helped me with this.
--thelma

: if ( $hh2 eq $hh1 ) # more clear AND faster!


: --
: Tad McClellan SGML consulting
: (e-mail address removed) Perl programming
: Fort Worth, Texas
 
J

Janek Schleicher

I am trying to compare two strings that must match except for their
spacing.

If I write:

$h1 = 'here i am';
$h2 = 'herei am';

can I do a less clumsy comparison than

($hh2 = $h2) =~ s/\s//x;
if($hh2 =~ /^$h1$/x) ...

You could also use the CPAN module String::Compare, e.g.:

use String::Compare;

my $h1 = 'here i am';
my $h2 = 'herei am';

if (String::Compare::chars_only($h1,$h2) eq 1) {
print "...";
}


Greetings,
Janek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top