Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl script: String comparison Ignoring spaces

0 views
Skip to first unread message

khan

unread,
Oct 15, 2008, 8:43:59 AM10/15/08
to
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

Leon Timmermans

unread,
Oct 15, 2008, 9:10:13 AM10/15/08
to
On Wed, 15 Oct 2008 05:43:59 -0700, khan wrote:
>
> 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

khan

unread,
Oct 15, 2008, 9:21:21 AM10/15/08
to

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

Jürgen Exner

unread,
Oct 15, 2008, 9:41:42 AM10/15/08
to
khan <musht...@gmail.com> wrote:
>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

Josef Moellers

unread,
Oct 15, 2008, 9:46:38 AM10/15/08
to

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

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Ted Zlatanov

unread,
Oct 15, 2008, 11:46:39 AM10/15/08
to
On 15 Oct 2008 13:10:13 GMT Leon Timmermans <faw...@gmail.com> wrote:

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

Tad J McClellan

unread,
Oct 15, 2008, 11:25:25 AM10/15/08
to
khan <musht...@gmail.com> wrote:

> spilt(/:/);
> if("jack" eq "$_[1])


Please post real Perl code.

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

cartercc

unread,
Oct 15, 2008, 3:05:31 PM10/15/08
to
On Oct 15, 8:43 am, khan <mushtaqk...@gmail.com> wrote:
> 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

Tim Greer

unread,
Oct 15, 2008, 4:15:10 PM10/15/08
to
khan wrote:

Just strip the leading and trailing white space, or all white space, and
then compare.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Andrzej Adam Filip

unread,
Oct 15, 2008, 4:22:26 PM10/15/08
to
khan <musht...@gmail.com> wrote:

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

spilt(/ *: */);

--
[pl>en Andrew] Andrzej Adam Filip : an...@onet.eu : an...@xl.wp.pl
I wasn't kissing her, I was whispering in her mouth.
-- Chico Marx

Leon Timmermans

unread,
Oct 16, 2008, 4:59:09 PM10/16/08
to
On Wed, 15 Oct 2008 10:46:39 -0500, Ted Zlatanov wrote:
> 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

0 new messages