LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


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

Rep: Reputation: 30
Perl String comparison


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, 12:04 PM   #2
dillybat
Member
 
Registered: Sep 2003
Location: Minnesota
Distribution: Red Hat 7.3, RHEL4 WS, FC 1-6, Ubuntu
Posts: 51

Rep: Reputation: 15
Re: Perl String comparison

Quote:
Originally posted by Xris718
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...
use to see if a string is empty or not
if ($string) { # string contains something
statement...
}

if (! $sting) { # string is emtpy
statement...
}

if ($var > 0 ) { #if variable is greater than 0, assuming is not a string
statement...
}
 
Old 04-01-2005, 12:09 PM   #3
dillybat
Member
 
Registered: Sep 2003
Location: Minnesota
Distribution: Red Hat 7.3, RHEL4 WS, FC 1-6, Ubuntu
Posts: 51

Rep: Reputation: 15
THE SCRIPT
#!/usr/bin/perl

my $string = "good-by";
my $twine = undef;
my $rope = "hello";

if ($string) {
print "string present\n";
}

if (! $twine) {
print "twine NOT present\n";
}

if ($string gt $rope) {
print "string bigger\n";
}
elsif ($rope gt $string) {
print "rope bigger\n";
}

THE OUTPUT:
string present
twine NOT present
rope bigger
 
Old 04-01-2005, 12:41 PM   #4
Xris718
Member
 
Registered: May 2003
Location: NYC
Distribution: CentOS
Posts: 261

Original Poster
Rep: Reputation: 30
Thanks guys
 
Old 04-01-2005, 12:52 PM   #5
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, 01:59 PM   #6
dillybat
Member
 
Registered: Sep 2003
Location: Minnesota
Distribution: Red Hat 7.3, RHEL4 WS, FC 1-6, Ubuntu
Posts: 51

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

if [! -z $string]; then
something....
I'm not to good at shell scripting but I believe that reads "if not zero length". If I am reading that right then look at my second posting with the script for an example... What you want is
Code:
if ($string)
.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
String Input & Array Comparison Problem azucarmom Programming 2 03-13-2005 07:23 AM
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 > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:18 PM.

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