PERL : check + or - sign in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL : check + or - sign in a variable
# 1  
Old 11-07-2011
PERL : check + or - sign in a variable

I have a variable $max = -3;
It can be $max = +3;

I need to check if this variable is a positive/negative value.
if its positive, should print "positive" if not "negative"

How can this be done?
Thanks in advance
# 2  
Old 11-07-2011
PERL

Hi,

Just check wheather the variable have value greater than or equal to zero then its positive else negative.
Code:
if ( $max >= 0 )
{
   print "Positive\n";
}
else
{
  print "Negative\n";
}

Note: Above works when the variable is integer scalar variable.

if it is string scalar variable, use regex.

Code:
if ( $max =~/^\+/ )
{
   print "Positive\n"
}
elsif ( $max =~/^\-/ )
{
  print "Negative\n";
}
else
{
   print "Positive\n";
}

This User Gave Thanks to rangarasan For This Post:
# 3  
Old 11-07-2011
Quote:
Originally Posted by rangarasan
...
if it is string scalar variable, use regex.
...
Looks like the integer test works with strings as well.

Code:
$
$ perl -le '$x = "0"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "1"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "9"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "+9"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "+0.99"; if ($x >= 0) {print "positive"} else {print "negative"}'
positive
$
$ perl -le '$x = "-0.99"; if ($x >= 0) {print "positive"} else {print "negative"}'
negative
$
$ perl -le '$x = "-1"; if ($x >= 0) {print "positive"} else {print "negative"}'
negative
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to escape colon sign from variable in shell?

Hello, Below script works fine when I manually enter required information for each file. When it comes to shell in auto mode, it gives various errors. I am under ubuntu 14.04 / trusty. manual_run.sh: #!/bin/bash /usr/bin/ffmpeg -start_at_zero -copyts -i nicki.mp4 -c:v mpeg2video \ -b:v 500k... (3 Replies)
Discussion started by: baris35
3 Replies

2. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Variable has value with = or == sign

I have a script which is reading value from property file and doing replacement in source location which contains multiple files. property file has values abc=xyz; yux=1;version=3.0; bcd=123;apple==mango when my script does search and reads value from property file. its replacing value... (1 Reply)
Discussion started by: Shamkamde
1 Replies

4. Shell Programming and Scripting

New to Perl Mail @ sign search and replace in a file

HI I'm terribly new to perl .. I;ve been trying to use this command to search and replace entries in a file I tried this and it works perl -p -i -e 's/old/new/' filename Problem is that I have a list of email addresses and I need to serach and replace the entire email address as my... (5 Replies)
Discussion started by: mnassiri
5 Replies

5. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

6. Shell Programming and Scripting

Perl Variable Check Script

I am working on a perl script that is used to update a list of hosts to a certain file but I am having an issue when I try to perform a check to make sure the user enters valid information. The following is what I have currently written for the script: IPINPUT: print "Enter IP Address: ";... (2 Replies)
Discussion started by: Takau
2 Replies

7. UNIX for Dummies Questions & Answers

Variable with @ sign is driving me crazy

Ok so I am working on a command that is going to do a Dig @ a certain IP address which is enclosed in a variable. Now I thought I had this figure out because it works. The problem is that it does not ALWAYS work. the variable is IP=192.168.1.1 the commands I have tried are dig... (2 Replies)
Discussion started by: MrEddy
2 Replies

8. Shell Programming and Scripting

Perl regular expressions don't like the @ ("at") sign.

Take a look at this code: #!/usr/bin/perl use 5.008; $_ = "somename@address.com"; if(/\@\w+\.com/) { print "\n\nmight be an email address\n\n"; } else { print "\n\nnot an email address\n\n"; } Shouldn't the /\@\w+\.com/ evaluate as true? I've also tried: ... (3 Replies)
Discussion started by: mrwatkin
3 Replies

9. Shell Programming and Scripting

Check if a variable is a number - perl

Logic of code if ( $var is a number ) { Do something } else { Do something else } My question is: How do I check if a variable is a number. All the reg ex that I came up with to match this is failing. Please help. (3 Replies)
Discussion started by: garric
3 Replies

10. Shell Programming and Scripting

Sign on/Sign off logging script

I'd like to make a script that I can execute every time I sign on to my linux box that keeps track of the time and allows to me to add a remark to a file. So basically once I log in, I run the script, and it outputs the date and time to a text file (log.txt). But that isn't my problem. I need... (1 Reply)
Discussion started by: Glider
1 Replies
Login or Register to Ask a Question