Perl script: String comparison Ignoring spaces

K

khan

Hi,

Iam new to Perl Script, am writing a perl script to read a
configuration file and take some actions accordingly. I read each line
of file, split the line in to variables and compare against the
predefined tokens. Comparison fails if variable in file has some
spaces around it.

Eg: Line read from file
Jhon: jack:hill;
spilt(/:/);
if("jack" eq "$_[1])
#Above comparison fails as $_[1] value is " jack"

Please let me know a solution to compare string variables ignoring
spaces around the variables.

Thanks,
-Mushtaq Khan
 
L

Leon Timmermans

Please let me know a solution to compare string variables ignoring
spaces around the variables.

Thanks,
-Mushtaq Khan

Fist of all, please don't use split in void or scalar context. It's
considered confusing and is deprecated.

The easiest solution is to just remove them during the splitting.
Something like this:

my @cols = split /\s?:\s?/;
if ($cols[1] eq "jack") {
...
}

Assuming there are no spaces at the start or end of the line, obviously.

Regards,

Leon Timmermans
 
K

khan

Please let me know a solution to compare string variables ignoring
spaces around the variables.
Thanks,
-Mushtaq Khan

Fist of all, please don't use split in void or scalar context. It's
considered confusing and is deprecated.

The easiest solution is to just remove them during the splitting.
Something like this:

my @cols = split /\s?:\s?/;
if ($cols[1] eq "jack") {
    ...

}

Assuming there are no spaces at the start or end of the line, obviously.

Regards,

Leon Timmermans

Thanks, Leon for replying. It's working fine.
 
J

Jürgen Exner

khan said:
Please let me know a solution to compare string variables ignoring
spaces around the variables.

See 'perldoc -q sapce':
How do I strip blank space from the beginning/end of a string?

jue
 
J

Josef Moellers

khan said:
Hi,

Iam new to Perl Script, am writing a perl script to read a
configuration file and take some actions accordingly. I read each line
of file, split the line in to variables and compare against the
predefined tokens. Comparison fails if variable in file has some
spaces around it.

Eg: Line read from file
Jhon: jack:hill;
spilt(/:/);
if("jack" eq "$_[1])
#Above comparison fails as $_[1] value is " jack"

Please let me know a solution to compare string variables ignoring
spaces around the variables.

Maybe you could use a regular expression:

if ($_[1] =~ /^\s*jack\s*$/) {
print "It's Jack all right\n";
}

If you must compare with a string contained in a variable, use the \Q
and \E qualifiers:

if ($_[1] =~ /^\s*\Q$name\E\s*$/) {
print "It's $name all right\n";
}

Oh ... TMTOWTDI:

split(/\s*:\s*/);
will take care of the colon as well as surrounding white space.
Then you can just use simple string comparisons with "eq" and "ne".

HTH,

Josef
 
T

Ted Zlatanov

LT> Fist of all, please don't use split in void or scalar context. It's
LT> considered confusing and is deprecated.

Why is split in scalar context considered confusing and deprecated? It
seems like a decent way to count the tokens in a word:

my $token_count = split ' ', $data;

You can do something similar with m/(\S+)/g I guess but it gets more
complicated when a token is not as easy to define as the token
separator sequence.

Ted
 
C

cartercc

I read each line
of file, split the line in to variables and compare against the
predefined tokens. Comparison fails if variable in file has some
spaces around it.

Eg: Line read from file
Jhon: jack:hill;
spilt(/:/);
if("jack" eq "$_[1])
#Above comparison fails as $_[1] value is " jack"

You can use the match operator, like this:

if ($_[1] =~ /jack/i) { do_something(); }

CC
 
T

Tim Greer

khan said:
Hi,

Iam new to Perl Script, am writing a perl script to read a
configuration file and take some actions accordingly. I read each line
of file, split the line in to variables and compare against the
predefined tokens. Comparison fails if variable in file has some
spaces around it.

Eg: Line read from file
Jhon: jack:hill;
spilt(/:/);
if("jack" eq "$_[1])
#Above comparison fails as $_[1] value is " jack"

Please let me know a solution to compare string variables ignoring
spaces around the variables.

Thanks,
-Mushtaq Khan

Just strip the leading and trailing white space, or all white space, and
then compare.
 
A

Andrzej Adam Filip

khan said:
Hi,

Iam new to Perl Script, am writing a perl script to read a
configuration file and take some actions accordingly. I read each line
of file, split the line in to variables and compare against the
predefined tokens. Comparison fails if variable in file has some
spaces around it.

Eg: Line read from file
Jhon: jack:hill;
spilt(/:/);
if("jack" eq "$_[1])
#Above comparison fails as $_[1] value is " jack"

Please let me know a solution to compare string variables ignoring
spaces around the variables.

You may treat spaces around separator (":") as part of separator.

spilt(/ *: */);
 
L

Leon Timmermans

Why is split in scalar context considered confusing and deprecated? It
seems like a decent way to count the tokens in a word:

Because it clobbers up @_.

Regards,

Leon Timmermans
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top