print ( "fred" gt "harvey" );

what does this print. How can we compare strings. If it was single characters, we can base our comparisons using ASCII.

Recommended Answers

All 5 Replies

print ( "fred" gt "harvey" );

what does this print. How can we compare strings. If it was single characters, we can base our comparisons using ASCII.

does anybody know a good forum to go to. I know that daniweb is good for java and c++

thanks!

print ( "fred" gt "harvey" );

what does this print. How can we compare strings. If it was single characters, we can base our comparisons using ASCII.

Quoting from the Camel book (Perl Programming, 2nd ed, p.87) on relational operators gt, ge, lt, le:

These operators return 1 for true, "" for false. String comparisons are based on the ASCII collating sequence, and, unlike in some languages, trailing spaces count in the comparison.

To answer your first question, then, what gets printed is an empty string, to wit, nothing, because the relationship is false. The string-compare works by comparing characters in the two strings in incrementing position-order until one of the strings either ends or has a not-the-same letter. My guess is that the C function strcmp() is used "under the hood", with its three-value result converted to true-or-false by the Perl test operator.

I ran your one-line snippet to see for myself what it would do, then played around with it with sprintf until it was reporting a value I could see. I often do that, write and run little test programs, when I'm not absolutely sure of what the language will do. I've been known, when that doesn't explain things adequately to me, to run the test-program in the Perl/Tk debugger (go to http://search.cpan.org, search on "ptkdb") so I can step it through and watch it in action. I suggest you do the same.

does anybody know a good forum to go to. I know that daniweb is good for java and c++

thanks!

Whats wrong with this forum? You didn't get a reply fast enough? This is a help forum with volunteer members, not tech support. Be patient.

Trust me, this is the best place to get help. Especially for such a simple problem.

Trust me, this is the best place to get help. Especially for such a simple problem.

Daniweb is a great forum, but for perl questions, www.perlmonks.com is considerably better. Also:

experts-exchange.com
tek-tips.com
perlguru.com
devshed.com

all have good perl forums, much more active than Daniwebs perl forum. But I really like Daniweb, which has one of the best webmasters (or webmistress) of any forum I have ever been a member of, and I have been a member of a lot of programming/IT forums.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.