Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

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

0
Europa MacDonald
Asked:
Europa MacDonald
  • 4
  • 4
2 Solutions
 
Adam314Commented:

my @groups = (0,0,0,0,0,0,0,0,0,0);
for($n2,$n2,$n3,$n4,$n5,$n6,$n7,$n8) {
	$groups[floor($_/100)]++;
}
print join(" ", @groups) . "\n";

Open in new window

0
 
Europa MacDonaldChief slayer of dragonsAuthor Commented:
could you adjust this so that it prints out to the text file with the other data please ?
0
 
Europa MacDonaldChief slayer of dragonsAuthor Commented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
my @groups = (0)x10;
$groups[$_/100]++ for $n2,$n2,$n3,$n4,$n5,$n6,$n7,$n8;
print OUT "@groups[0..9]\n";
0
 
Europa MacDonaldChief slayer of dragonsAuthor Commented:
delineated by a tab :-)

thankyou very much for your help
0
 
ozoCommented:
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";
0
 
ozoCommented:
print OUT "\t$n2\t$n3\t$n4\t$n5\t$n6\t$n7\t$n8 avg value ",(floor $avg),"\t@groups[0..9]\n";
0
 
Europa MacDonaldChief slayer of dragonsAuthor Commented:
thanks again guys
0
 
ozoCommented:
print OUT join"\t","",$n2,$n3,$n4,$n5,$n6,$n7,"$n8 avg value ".floor($avg),"@groups[0..9]\n";
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now