Solved

Sorting numbers into groups with perl

Posted on 2009-07-07
9
130 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now