Problem with string comparison

Discussion in 'Perl Misc' started by mark, Oct 5, 2006.

  1. mark

    mark Guest

    Hello,

    I have following program in linux:

    open(DATA, "/etc/hostname");
    $hostname = <DATA>;
    print $hostname
    if ($hostname eq "debian01") {
    print "OK";
    }
    close(DATA);

    Now, when I execute it I just got:
    debian01
    so it seems that $hostdata = "debian01" but there is no 'OK' string :/.
    What is wrong?!? How can I make it working?!?

    Regards, mark
     
    mark, Oct 5, 2006
    #1
    1. Advertising

  2. mark wrote:
    >
    > I have following program in linux:
    >
    > open(DATA, "/etc/hostname");
    > $hostname = <DATA>;
    > print $hostname
    > if ($hostname eq "debian01") {
    > print "OK";
    > }
    > close(DATA);
    >
    > Now, when I execute it I just got:
    > debian01
    > so it seems that $hostdata = "debian01"


    No, it is actually "debian01\n". You have to use chomp to remove the newline
    first:

    perldoc -f chomp


    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Oct 5, 2006
    #2
    1. Advertising

  3. mark

    Matt Garrish Guest

    Mirco Wahab wrote:

    > Thus spoke mark (on 2006-10-05 01:26):
    >
    > > I have following program in linux:
    > > ...
    > > Now, when I execute it I just got:
    > > debian01
    > > so it seems that $hostdata = "debian01" but there is no 'OK' string :/.
    > > What is wrong?!? How can I make it working?!?

    >
    > > open(DATA, "/etc/hostname");
    > > $hostname = <DATA>;
    > > print $hostname
    > > if ($hostname eq "debian01") {
    > > print "OK";
    > > }
    > > close(DATA);

    >
    > there are some problems, most
    > will be found by Perl if you
    > start your program with:
    >
    > use warnings;
    > use strict;
    > ...
    >
    > To read the content into a scalar,
    > you could 'slurp' the file, but
    > for beginners, its better to read
    > into an array and take the first
    > element as the first line:
    >
    > open(my $data, '<', '/etc/hostname') or die "problem: $!";
    > my @lines = <$data>;
    > my $hostname = $lines[0];
    > close $data;
    >


    You're assuming that there's more in the file and that he's trying to
    slurp (I don't see $/ being set to undef in his code). It's common
    practice to read the first line into a scalar if, for example, the file
    contains a count / timestamp / some other single value that's being
    persisted. Using an array is just wasteful extra step, even for
    beginners.

    > To compare strings, you don't need
    > always a string comparison operator:
    >
    > if ( $hostname =~ /debian01/ ) {
    > print "OK";
    > }
    >
    > means: if the string 'debian01' is found
    > somewhere on the first line pulled from
    > the array. Otherwise, it wouldn't work
    > because the "\n" is still in the string.
    >


    Or you just chomp your line and use an equality check. You're
    contradicting what you wrote above by giving this advice, though.
    Regular expression checking requires much more careful consideration
    than most beginners generally give them, so if you're assuming a
    beginner audience it seems odd you'd provide this example instead of
    just mentioning chomp (for example, what happens to the OP if the first
    line actually contains "debian011a").

    Matt
     
    Matt Garrish, Oct 5, 2006
    #3
  4. mark <> wrote:

    > I have following program in linux:
    >
    > open(DATA, "/etc/hostname");



    You are missing a check of the return value to see if you
    actually got what you asked for.


    > $hostname = <DATA>;



    You are missing a chomp().


    > print $hostname



    You are missing a semicolon.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 5, 2006
    #4
  5. mark

    mark Guest

    Hello,

    Thank you - chomp was the thing I needed :) .

    Regards, mark
     
    mark, Oct 5, 2006
    #5
    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. Senthoorkumaran Punniamoorthy

    String comparison problem

    Senthoorkumaran Punniamoorthy, Apr 7, 2004, in forum: Python
    Replies:
    2
    Views:
    371
    Piet van Oostrum
    Apr 7, 2004
  2. Replies:
    21
    Views:
    1,430
    Alex Vinokur
    Aug 18, 2007
  3. Smithers
    Replies:
    12
    Views:
    1,191
    Ben Voigt [C++ MVP]
    Jul 7, 2009
  4. Lucas Holland

    Problem with string comparison

    Lucas Holland, Dec 19, 2006, in forum: Ruby
    Replies:
    3
    Views:
    99
    Lucas Holland
    Dec 19, 2006
  5. Deepu
    Replies:
    1
    Views:
    250
    ccc31807
    Feb 7, 2011
Loading...

Share This Page