Sponsored Content
Full Discussion: Perl Script Integer Test
Top Forums Shell Programming and Scripting Perl Script Integer Test Post 303000219 by LinQ on Thursday 6th of July 2017 01:57:55 PM
Old 07-06-2017
Perl Script Integer Test

Working out a small problem, I have a need of a Perl snippet which might look something like this:
Code:
use integer;

...

if ($changingNumber / 2)
{
    do something;
}

else
{
    do something else;
}

...

What I want to happen is for "if" to resolve as "true" every time a whole number is produced by the quotient of "$changingNumber / 2"; allowing the "do something" block only then to be executed.

But what I get, instead, is the eval of "true" and the consequent "do something" block execution for every value of "$changingNumber" of 2 and greater.

I know this is probably a simple error on my part, but I can't quite elucidate what would do the trick here...

Any ideas?

Thanks!
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Integer/Float Script Problem

Hi, I have a script which takes a value from a file and performs calculations on it. Trouble is that this value is a float not an integer and it errors at the decimal point! eg. 94.62 I would like to be able to detect the length of the float (in this above case, 5 characters), and simply do a... (2 Replies)
Discussion started by: danhodges99
2 Replies

2. Programming

C function to test string or integer

Hi everyone , Is there any predefined C function that tests whether an input is string or an integer? Thank's in advance :) (3 Replies)
Discussion started by: qqq
3 Replies

3. UNIX for Advanced & Expert Users

test the string is char or integer

How will test the string contains numeric character or alphabet, is there any script to test ? (10 Replies)
Discussion started by: rajesh08
10 Replies

4. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

5. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

6. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

7. Shell Programming and Scripting

PERL - traverse sub directories and get test case results

Hello, I need help in creating a PERL script for parsing test result files to get the results (pass or fail). Each test case execution generates a directory with few files among which we are interested in .result file. Lets say Testing is home directory. If i executed 2 test cases. It will... (4 Replies)
Discussion started by: ravi.videla
4 Replies

8. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

9. Shell Programming and Scripting

Perl: backslash in front of integer like \32768

In Perl, what does a backslash preceding an integer do like \32768 ? The $/ section of perlvar writes: local $/ = \32768; # or \"32768", or \$var_containing_32768 How is \32768 different from just 32768 without backslash? I do not understand the backslashes in \"32768" and... (1 Reply)
Discussion started by: LessNux
1 Replies

10. Shell Programming and Scripting

Perl inserting random negative integer

Hi All, i have problem here whenever i run this perl script that is pasted here, it inserts a negative number in place of PO_nbr . What the script does is reads a pipe delimited file and then using some values on the file it will query db to get few other values and then it inserts the... (4 Replies)
Discussion started by: selvankj
4 Replies
RateLimiter(3pm)					User Contributed Perl Documentation					  RateLimiter(3pm)

NAME
Schedule::RateLimiter - prevent events from happening too quickly. SYNOPSIS
use Schedule::RateLimiter; # Don't let this event happen more than 5 times in a 60 second period. my $throttle = Schedule::RateLimiter->new ( iterations => 5, seconds => 60 ); # Cycle forever, but not too fast. while ( 1 ) { $throttle->event(); &do_something; } DESCRIPTION
This module provides a way to voluntarily restrict how many times a given action may take place within a specified time frame. Such a tool may be useful if you have written something which periodically polls some public resource and want to ensure that you do not overburden that resource with too many requests. Initially, one might think that solving this problem would be as simple as sleeping for the number of seconds divided by the number of iterations in between each event. However, that would only be correct if the event took no time at all. If you know exactly how much time each event is going to take then you could build an even more complicated one-liner such as this: sleep( (seconds / iterations) - single_event_time ) This module is intended to address the other cases when the exact run-time of each event is unknown and variable. This module will try very hard to allow an event to happen as many times as possible without exceeding the specified bounds. For example, suppose you want to write something that checks an 'incoming' directory once a minute for files and then does something with those files if it finds any. If it takes you two seconds to process those files, then you want to wait 58 seconds before polling the directory again. If it takes 30 seconds to process those files, then you only want to wait 30 seconds. And if it takes 3 minutes, then you want to poll the directory again immediately as soon as you are done. my $throttle = Schedule::RateLimiter->new ( seconds => 60 ); &poll_and_process while ( $throttle->event ); METHODS
" new() " Creates and returns a new Schedule::RateLimiter object. The constructor takes up to three parameters: o block (default: true) This parameter accepts a true or false value to set the default "block" behavior on future calls to event(). It makes it more convenient to turn blocking off for an entire object at a time. o iterations (default: 1) This specifies the number of times an event may take place within the given time period. This must be a positive, non-zero integer. o seconds (required) This specifies the minimum number of seconds that must transpire before we will allow (iterations + 1) events to happen. A value of 0 disables throttling. You may specify fractional time periods. example: my $throttle = Schedule::RateLimiter->new ( iterations => 2, seconds => 10 ); # Event 1 $throttle->event(); # Event 2 $throttle->event(); # Event 3 $throttle->event(); # 10 seconds will have transpired since event 1 at this point. # Event 4 $throttle->event(); # 10 seconds will have transpired since event 2 at this point. " event() " Called to signal the beginning of an event. This method will return true or false to indicate if it is ok to proceed with the event. This method uses Time::HiRes to do its calculations and sleeping, so the precision of this method will be the same as the precision of Time::HiRes on your platform. Takes one (optional) parameter: o block (default: true) If set to a false value, this method will do a non-blocking check to see if it is ok for the event to occur. If it is not ok, this method will return a false value and assume that the event did not take place. Otherwise, this method will return a true value and assume that the event did take place. example: # Stop when the code moves too fast. while ( 1 ) { if ($throttle->event( block => 0 )) { &do_something; } else { die 'I went too fast!'; } } BUGS
This module needs to keep a record of when every iteration took place, so if you are allowing a large number of iterations to happen in the given time period, this could potentially use a lot of memory. KNOWN ISSUES
If you have multiple iterations that typically happen very quickly, and you want to limit them in a long period of time, they will "clump" together. That is, they all happen at just about the same time, and then the system waits for a long period before doing the same "clump" again. That's just the nature of the best-fit algorithm. Anything that is done to try to separate single events with longer waits than necessary will potentially create a sub-optimal situation if an event in the future takes longer than expected. If you really want all of your events to start at even time periods apart from each other, then set the number of iterations to 1 and adjust the number of seconds accordingly. AUTHOR
Daniel J. Wright, <wright@pair.com> SEE ALSO
The POE module provides a more heavyweight solution to this problem as well. perl. perl v5.10.0 2003-12-04 RateLimiter(3pm)
All times are GMT -4. The time now is 08:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy