Integers/floating points in Perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Integers/floating points in Perl
# 1  
Old 02-10-2010
Integers/floating points in Perl

Hi all

I am trying to figure out how to specify integers and floating points specifically in a Perl program.

Basically the idea is that I want the user to input an integer and then dependent on that value they get a specific output. Now my problem is that I don't know how to write the content of the parentheses of either of the two following commands:

if ( input != integer ) or
if ( input = floating point )

I basically have the rest of the program down but haven't been able to figure this small part out.
How do I tell Perl that if the user inputs a floating point number then it should print an error message?

Any ideas?
# 2  
Old 02-10-2010
Consider the input as a string. You could strip out all characters [0-9] and if there's anything left over, they've entered something they shouldn't have.
# 3  
Old 02-10-2010
Hey again

I tried looking up stripping both in the Perl manual and on Google, but didn't really understand how to use that information.

I found something like this:

Code:
for ($string) {
        s/^\s+//;                     I am guessing this means before
        s/\s+$//;                     and this after, but what is the ^ for?
    }

for removing spaces before and after the string.
My problem is that I can't find any information about what the different parts mean.
I am wondering if in my case I can use something like this:

Code:
for ($string) {
        s/0\s//;
        s/1\s//;
    }

and fill it out for 2-9 too or maybe make a loop inside the for loop to take the 10 numbers by turn.
And then afterwards use: if ($string = "" ) : continue program, else : error message.

Last edited by pludi; 02-10-2010 at 01:33 PM.. Reason: code tags, please...
# 4  
Old 02-10-2010
Ah, you're totally new to perl?

Code:
if( $val =~ /^\d+$/ )
{
  # Is a decimal
}
else
{
  # Is a float or other invalid input
}

# 5  
Old 02-10-2010
Oki cool thanx.

It worked as intended.
But now I'm going to be really annoying since, yes I'm a total newbie. But I like to understand what the code I use actually does, since then it's a lot easier to rewrite to something else later.

if( $val =~ /^\d+$/ )

What does the ~ /.../ part mean?
The ^ should be the beginning of the string
and the $ should be the end of the string
and \d+ represents any integer/decimal in the string.
# 6  
Old 02-10-2010
Quote:
Originally Posted by Banni
What does the ~ /.../ part mean?
It means "here be a regular expression". That's one of the defining features of Perl.
Quote:
Originally Posted by Banni
The ^ should be the beginning of the string
and the $ should be the end of the string
Right, and right.
Quote:
Originally Posted by Banni
and \d+ represents any integer/decimal in the string.
It means "at least one decimal (0-9)", no punctuations, letters, or other cruft.
# 7  
Old 02-10-2010
=~ is the regular expression operator. A regular expression can do lots of different things, but is often just used to compare. The syntax of the regex is kind of similar to sed's but with significant differences too.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Integers (I think)

Hi, I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)
Discussion started by: Rhysers
8 Replies

2. Shell Programming and Scripting

Trouble converting columns of integers to floating decimals

I have a document that has 7 columns. eg. $1 $2 $3 $4 $5 $6 $7 string string string string integer integer integer The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all values are floating decimals w/4... (1 Reply)
Discussion started by: kadm
1 Replies

3. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

4. Shell Programming and Scripting

perl : replace multiline text between two marker points

Hi there I just wondered if someone could give me some perl advice I have a bunch of text files used for a wiki that have common headings such as ---++ Title blah ---++ Summary blah ---++ Details Here is the multiline block of text I wish to (6 Replies)
Discussion started by: rethink
6 Replies

5. Shell Programming and Scripting

Cancel down 2 integers

Wonderful evening to all of you! My problem has to possible starting points. Well, not really, but getting to either one is no problem at all. So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080. When I am done, I would like to have an output... (5 Replies)
Discussion started by: jakunar
5 Replies

6. Shell Programming and Scripting

PERL- converting exponent value to floating point

Hi Friends, I've an exponent value like, $val="9.57669e-05"; I want to convert this value to floating point value in PERL scripting. I tried googling for the solution, and also asked many perl friends. Unfortunately, I didn't get answer. Could you please help me? Thanks in advance... (4 Replies)
Discussion started by: ganapati
4 Replies

7. Shell Programming and Scripting

Use Perl In Bash Script To Compare Floationg Points

Is there a way to compare two floating points numbers in a bash script using perl? I've tried just using a bash if statement and it doesn't seem to support floating point numbers. Can the perl line read vars from bash then output a var to bash? a=1.1 #from bash b=1.5 #from bash if... (3 Replies)
Discussion started by: Grizzly
3 Replies

8. Programming

Using write() with integers in C

I'm trying to write an integer to a file using the write() function, but write() requires the parameter to be written to be a const void*. How would I go about doing this? also: using itoa() produces a " warning: implicit declaration of function 'itoa' " even though i have #included stdlib.h (2 Replies)
Discussion started by: h@run
2 Replies

9. Shell Programming and Scripting

integers in the if statement

hi, im trying to compare two variables in csh to put in an if statement, eg: set a = $firstnum set b = $secondnum if ($a -ge $b) echo $a But I get an error ("if: Expression syntax"). How can I make csh see my variables as integers? thanks in advance! (5 Replies)
Discussion started by: Deanne
5 Replies

10. UNIX and Linux Applications

Gnuplot question: how to plot 3D points as colored points in map view?

I have a simple gnuplot question. I have a set of points (list of x,y,z values; irregularly spaced, i.e. no grid) that I want to plot. I want the plot to look like this: - points in map view (no 3D view) - color of each point should depend on its z-value. - I want to define my own color scale -... (0 Replies)
Discussion started by: karman
0 Replies
Login or Register to Ask a Question