Solved

Sorting numbers into groups with perl

Posted on 2009-07-07
9
132 Views
Last Modified: 2012-05-07
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
Comment
Question by:MichaelGlancy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 300 total points
ID: 24794512

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
 

Author Comment

by:MichaelGlancy
ID: 24795567
could you adjust this so that it prints out to the text file with the other data please ?
0
 

Author Comment

by:MichaelGlancy
ID: 24795612
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
Independent Software Vendors: 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!

 
LVL 84

Expert Comment

by:ozo
ID: 24795613
my @groups = (0)x10;
$groups[$_/100]++ for $n2,$n2,$n3,$n4,$n5,$n6,$n7,$n8;
print OUT "@groups[0..9]\n";
0
 

Author Comment

by:MichaelGlancy
ID: 24795620
delineated by a tab :-)

thankyou very much for your help
0
 
LVL 84

Expert Comment

by:ozo
ID: 24795634
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 24795638
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
 

Author Closing Comment

by:MichaelGlancy
ID: 31600600
thanks again guys
0
 
LVL 84

Expert Comment

by:ozo
ID: 24795748
print OUT join"\t","",$n2,$n3,$n4,$n5,$n6,$n7,"$n8 avg value ".floor($avg),"@groups[0..9]\n";
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question