Link to home
Start Free TrialLog in
Avatar of Europa MacDonald
Europa MacDonaldFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sorting numbers into groups with perl

hello again

I have a random selection of numbers from 1 to 999. I have to sort these into 10 groups.
1-99, 100-199, 200-299, 300-399, 400-499, 500-599, 600-699, 700-799, 800-899, and 900-999

a typical selection of values would be 217 345      354      654      757      890      897.

all these values are read from a file, and inserted into scalar variables.
so i need to examine the first  scalar variable, determine which group its value falls into and tally that group a 1.
If another subsequent number fell into that group, it would have to be tallied another 1, which would give it a value of 2, and so on.

the required output for that example set would be

0 0 1 2 0 0 1 1 2 0

does this make sense ? It has to be inside the code I already have, shown below.

use strict;
use warnings;
 
##### Opens input file to variables and outputs to file
 
open(IN, "<in.txt") or die;
open(OUT, ">out.txt") or die;
my $s;
 
while($s = <IN>) {
   if($s =~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
      my ($n1, $n2, $n3, $n4, $n5, $n6, $n7, $n8) = ($1, $2, $3, $4, $5, $6, $7, $8);
 
##### Calculations
 
	my $avg = ($n2 + $n3 + $n4 + $n5 + $n6 + $n7 + $n8)/ 7;
 
##### sort values ascending order
 
	($n2,$n3,$n4,$n5,$n6,$n7,$n8)=sort{$a<=>$b}$n2,$n3,$n4,$n5,$n6,$n7,$n8;
 
##### calculate value difference
 
	my $vd1 = ($n2 - 0);
	my $vd2 = ($n3 - $n2);
	my $vd3 = ($n4 - $n3);
	my $vd4 = ($n5 - $n4);
	my $vd5 = ($n6 - $n5);
	my $vd6 = ($n7 - $n6);
	my $vd7 = ($n8 - $n7);
 
	my $avgdif = ($vd1 + $vd2 + $vd3 + $vd4 + $vd5 + $vd6 + $vd7) / 7;
 
	use POSIX;
 
	print OUT "\t$n2\t$n3\t$n4\t$n5\t$n6\t$n7\t$n8 avg value ",(floor $avg),"\n";
 
##### print out value differences
 
	print OUT "\t$vd1\t$vd2\t$vd3\t$vd4\t$vd5\t$vd6\t$vd7 avg diff ",(floor $avgdif),"\n\n";
 
   }
}
 
##### this last bit is here to stop the program closing until you hit return
 
	END {
    	print "Press ENTER to close: ";
    	''.<STDIN>
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Europa MacDonald

ASKER

could you adjust this so that it prints out to the text file with the other data please ?
at the moment, for this data input :

454,345,217,897,654,354,890,757,867,567
879,123,768,489,165,346,899,55,344,322

I get

      217      345      354      654      757      890      897       avg value       587
      217      128      9      300      103      133      7       avg diff               128

      55      123      165      346      489      768      899       avg value       406
      55      68      42      181      143      279      131       avg diff               128

if I could get it like this;
      217      345      354      654      757      890      897       avg value       587       0 0 1 2 0 0 1 1 2 0
      217      128      9      300      103      133      7       avg diff               128

      55      123      165      346      489      768      899       avg value       406       0 0 1 2 0 0 1 1 2 0
      55      68      42      181      143      279      131       avg diff               128

that would be great.
Avatar of ozo
my @groups = (0)x10;
$groups[$_/100]++ for $n2,$n2,$n3,$n4,$n5,$n6,$n7,$n8;
print OUT "@groups[0..9]\n";
delineated by a tab :-)

thankyou very much for your help
print OUT "\t$n2\t$n3\t$n4\t$n5\t$n6\t$n7\t$n8 avg value ",(floor $avg),"\n@groups[0..9]\n";
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks again guys
print OUT join"\t","",$n2,$n3,$n4,$n5,$n6,$n7,"$n8 avg value ".floor($avg),"@groups[0..9]\n";