Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Percentage different 1

Status
Not open for further replies.

travs69

MIS
Dec 21, 2006
1,431
US
I am currently doing a percentage of difference calculation like
$calc = ($value/$expected)*100
which works perfect as long as $value is actually less than $expected.

If it is greater $calc moves to greater than 100%.
Should I just have two thresholds I should check on

if ($calc <= 90 || $calc >= 110)

or is there a way to make it so even greater differences comes out as a percentage of difference less than 100
maybe:

$calc = $value < $expected ? $value/$expected : $expected/$value

or is there something easier I'm missing?


i.e.
actual/expect calc
90/100 = 90%
99/100 = 99%
110/100 = 90%
101/100 = 99%


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
101 is 101% of 100, not 99%. If you want to always use the larger value as the divisor you will have to compare them before plugging them into a formual to check the percentage.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok.. I thought maybe I was missing something.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Code:
#!/usr/bin/perl
use strict;

# Open the switch text for reading
open(SWITCH, "<switch.txt") or die "Could not open switch.txt:$!";
my @switch = <SWITCH>;
close SWITCH;
chomp @switch;
# Open the master text for searching
open(MASTER, "<master.txt") or die "Could not open master.txt:$!";
my @master = <MASTER>;
close MASTER;
chomp @master;


# Open an output file
open(OUTPUT, ">output.txt") or die "Could not open output.txt:$!";

# Loop through the switch text
for my $TheRec (@switch) {

# The three variables in the exported switch files are delimited by tabs
# Cut them up to get the needed address variable
   (my $status, my $smac, my $port) = split(/\s+/, $TheRec, 3);
	print "S: $smac\n";
# While still in that loop check the master.txt file for a match
   for my $TheRec2 (@master) {


	# Once again the variables needed are tab delimited
	# Cut them up into appropriate variables
      (my $interface, my $unknown, my $unknown2, my $mmac, my $mip, my $type) = split(/\s+/, $TheRec2, 6);
	  print "M: $mmac\n";
	  
		# Get rid of any spaces in the variables
		($status, $smac, $port, $interface, $mmac, $mip, $type) =~ s/^\s+|\s+$//g;

		# Compare the switch mac with the master mac
		# If they match then export all of the relevant data
		# to the output file delimited by tabs
		if($smac eq $mmac) {
			print OUTPUT "$smac\t$mip\t$port\n";
		}

# End the MASTER search loop
   }



# End the SWITCH search loop
}

# Close all of the used files
close OUTPUT;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
oops.. wrong place :(

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top