Perl script: String comparison Ignoring spaces

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

  1. khan

    khan Guest

    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
    khan, Oct 15, 2008
    #1
    1. Advertising

  2. 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
    Leon Timmermans, Oct 15, 2008
    #2
    1. Advertising

  3. khan

    khan Guest

    On Oct 15, 6:10 pm, Leon Timmermans <> wrote:
    > 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


    Thanks, Leon for replying. It's working fine.
    khan, Oct 15, 2008
    #3
  4. khan <> 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
    Jürgen Exner, Oct 15, 2008
    #4
  5. khan wrote:
    > 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

    --
    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
    Josef Moellers, Oct 15, 2008
    #5
  6. khan <> wrote:

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



    Please post real Perl code.



    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Oct 15, 2008
    #6
  7. khan

    Ted Zlatanov Guest

    On 15 Oct 2008 13:10:13 GMT Leon Timmermans <> 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
    Ted Zlatanov, Oct 15, 2008
    #7
  8. khan

    cartercc Guest

    On Oct 15, 8:43 am, khan <> 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
    cartercc, Oct 15, 2008
    #8
  9. khan

    Tim Greer Guest

    khan wrote:

    > 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.
    --
    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!
    Tim Greer, Oct 15, 2008
    #9
  10. khan <> wrote:

    > 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(/ *: */);

    --
    [pl>en Andrew] Andrzej Adam Filip : :
    I wasn't kissing her, I was whispering in her mouth.
    -- Chico Marx
    Andrzej Adam Filip, Oct 15, 2008
    #10
  11. 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
    Leon Timmermans, Oct 16, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,793
  2. Sandeep
    Replies:
    1
    Views:
    884
    Raymond DeCampo
    Jan 15, 2006
  3. John B. Matthews
    Replies:
    4
    Views:
    667
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    623
  5. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,596
    Jan C. Faerber
    Sep 10, 2009
Loading...

Share This Page