LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2005, 10:31 AM   #1
Xris718
Member
 
Registered: May 2003
Location: NYC
Distribution: CentOS
Posts: 261

Rep: Reputation: 30
Perl string comparisons


Hi

I need to know how to do string comparisons in perl just like shell does. How do you convert the following into perl?
ie:

if [-n "$string"]; then
statement.....

if [-z "$string"]; then
statement...

if [$var -gt 0]; then
statement...
 
Old 04-01-2005, 11:20 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
I think this is what you want:
Code:
if($string){
 print "Variable exists\n";
}
if(!$string){
 print "Variable is empty\n";
}
if($string>0){
 print "Variable greater than 0\n";
}
 
Old 04-01-2005, 12:42 PM   #3
Xris718
Member
 
Registered: May 2003
Location: NYC
Distribution: CentOS
Posts: 261

Original Poster
Rep: Reputation: 30
thank you i think thats it!
 
Old 04-01-2005, 01:06 PM   #4
Xris718
Member
 
Registered: May 2003
Location: NYC
Distribution: CentOS
Posts: 261

Original Poster
Rep: Reputation: 30
oh what about this convert into perl?
ie:

if [! -z $string]; then
something....
 
Old 04-01-2005, 02:42 PM   #5
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
You might be interested in the "defined" function.

Code:
if($string) {
    # string exists and evaluates as true
} elsif(defined $string) {
    # string exists but evaluates as false
} else {
    # string doesn't exist
}
Also, you can use numerical comparisons directly on strings, if they consist of just a number Perl will treat them as a number. On the other hand, you can use the "eq" operator to test equality as a string. (All the numerical comparison operators have string versions)

Code:
$number = 2;
$number_string = "02";

if ($number == $number_string) {
    print "Variables are numerically equal.\n";
}
if ($number eq $number_string) {
    print "Variables are equivalent strings.\n";
}
Run this and you'll see that the first statement is printed but not the second.
 
Old 04-03-2005, 10:43 AM   #6
MetaPhase
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Rep: Reputation: 0
Question How are "!-z" and "-n" different?

Quote:
Originally posted by Xris718
oh what about this convert into perl?
ie:

if [! -z $string]; then
something....
How is the test:
Code:
[! -z $string]
different from the test
Code:
 [-n $string]
?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl String comparison Xris718 Linux - General 5 04-01-2005 01:59 PM
perl string comparison problem AM1SHFURN1TURE Programming 3 03-06-2005 10:29 AM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM
loop through string with perl lfur Programming 2 07-03-2004 08:05 AM
How to separate a string in Perl Bassam Programming 1 06-03-2004 01:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration