Perl script: String comparison Ignoring spaces

Discussion in 'Perl Misc' started by khan, Oct 15, 2008.

  1. khan

    khan Guest


    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;
    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.

    -Mushtaq Khan
    khan, Oct 15, 2008
    1. Advertisements

  2. 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.


    Leon Timmermans
    Leon Timmermans, Oct 15, 2008
    1. Advertisements

  3. khan

    khan Guest

    Thanks, Leon for replying. It's working fine.
    khan, Oct 15, 2008
  4. See 'perldoc -q sapce':
    How do I strip blank space from the beginning/end of a string?

    Jürgen Exner, Oct 15, 2008
  5. 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:

    will take care of the colon as well as surrounding white space.
    Then you can just use simple string comparisons with "eq" and "ne".


    Josef Moellers, Oct 15, 2008

  6. Please post real Perl code.
    Tad J McClellan, Oct 15, 2008
  7. khan

    Ted Zlatanov Guest

    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 Zlatanov, Oct 15, 2008
  8. khan

    cartercc Guest

    You can use the match operator, like this:

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

    cartercc, Oct 15, 2008
  9. khan

    Tim Greer Guest

    Just strip the leading and trailing white space, or all white space, and
    then compare.
    Tim Greer, Oct 15, 2008
  10. You may treat spaces around separator (":") as part of separator.

    spilt(/ *: */);
    Andrzej Adam Filip, Oct 15, 2008
  11. Because it clobbers up @_.


    Leon Timmermans
    Leon Timmermans, Oct 16, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.