Solved

Sorting numbers into groups with perl

Posted on 2009-07-07
9
125 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

759 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

21 Experts available now in Live!

Get 1:1 Help Now