Problem with string comparison

M

mark

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
 
J

John W. Krahn

mark said:
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
 
M

Matt Garrish

Mirco said:
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
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top