Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with string comparison

Reply
Thread Tools

Problem with string comparison

 
 
mark
Guest
Posts: n/a
 
      10-04-2006
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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-05-2006
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
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      10-05-2006

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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-05-2006
mark <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
mark
Guest
Posts: n/a
 
      10-05-2006
Hello,

Thank you - chomp was the thing I needed .

Regards, mark

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
False Positives From String Comparison using string.Equals() Smithers ASP .Net 12 07-07-2009 01:23 AM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Is there systematic performance comparison of std::string and c style string? yu_kuo@sina.com C++ 21 08-18-2007 04:32 PM
String comparison problem Senthoorkumaran Punniamoorthy Python 2 04-07-2004 09:36 PM



Advertisments