Improve company productivity with a Business Account.Sign Up

x
Solved

# What's strategy to write this simple prog. in perl ?

Posted on 1998-11-07
Medium Priority
209 Views
Last Modified: 2010-03-04
I have a file which contain lots of numbers. I want to find the frequancy of each range.Can somebody
help me with the strategy to write this program. -the file contain numbers say 1-1000 so the min and max are known in advance
-the user input the width eg. 10 so there should be 1000/10 =100 number of classes (width=user input variable)
the program should search and find the frequency between numbers b/w 0-199, 200-299, 300-399 until 1000 so the result should be
class       Lower limit    upper limit  frequency      Prop
0           0.000           199.000      120         0.0987

Thanks alot for you help
0
Question by:aja101498
• 6
• 5
12 Comments

LVL 85

Expert Comment

ID: 1206029
How do you get 0-199, 200-299, 300-399 out of 1-1000?
0

Author Comment

ID: 1206030
Lower limit = 0
Upper limit = 1000 both lower & upper limits are known in advance)

Divide the n(1000)/ width(10). The class width is chosen by users and the n is known in advance.  1000/10= 100 so you get the size of each class:

class 1 0-199 (100),
class 2 200-299 (100),
class 3 300-399 (100)
and so forth.
Thanks
0

LVL 5

Accepted Solution

b2pi earned 200 total points
ID: 1206031
Assuming that the numbers are in a file, one per line: (And note the almost total lack of error checking)

my(\$Lower) = 1;
my(\$Upper) = 1000;
my(\$Width) = 200;
my(%Buckets);
my(\$buck);

open(FIL, "<\$filename") || die "Unable to open \$filename: \$!";
while(<FIL>) {
\$buck = int((\$_ - \$Lower)/\$Width);
\$Buckets{\$buck}->{count} ++;
if (!defined(\$Buckets{\$buck}->{high} or (\$_ > \$Buckets{\$buck}->{high})) {
\$Buckets{\$buck}->{high} = \$_;
}
if (!defined(\$Buckets{\$buck}->{low} or (\$_ < \$Buckets{\$buck}->{low})) {
\$Buckets{\$buck}->{low} = \$_;
}
}
close(FIL);

Does all that make sense?

0

Author Comment

ID: 1206032
: b2pi,

Can you please comment it. I want to understand it and then I will post the grade. Thanks a million.
0

Author Comment

ID: 1206033
b2pi

What variable holds the frequency ?
and classes. Is it buck ?

Yes the numbers are in a file, one per line. Thanks
0

LVL 5

Expert Comment

ID: 1206034
Define frequency, please.

The count of the number of items held in a particular bucket, if that bucket number is n, is in \$Buckets{n}->{count}

All the code is  doing is running through the file, one line at a time.  It reads the number from the file, and

1.) increments the counter for the appropriate bucket.
2.) updates the high or low values, as appropriate.
0

Author Comment

ID: 1206035
Sorry.You know my friend I know very little about Perl that's why I am asking this question so please bear with me. I appreciate your help. Thanks

Frequency:
It is how many number b/w 0.000 and 199.000. Sometimes there are 50 numbers in one range or class and sometimes there are 0 numbers in a class.
Let us say in range or class between 0-199, there are only the following numbers:

20 X the number 0
10 X the number 88
100 X the number 120
1 X the number 7

then
class       Lower limit    upper limit  frequency    averagr
0           0.000           199.000      131        131/3

So in other words it is the total number of all the numbers between 0.000 - 199.000

X: times
the first column class is just the serial number of classes. It start from 0 to the 9 if the n=1000 and width=10

Thanks
0

LVL 5

Expert Comment

ID: 1206036
OK, so I'm using the word 'count', and you're using the word frequency. (BTW, I've assumed thoughout that your numbers are always positive, i.e. that \$Lower is > 0)

Also, it appears that I misunderstood your definitions of upper and lower, and that the min/max code I threw in there is uneccessary.

To figure out the limits, the salient piece of code is the

int(\$_ - \$Lower)/\$Width

\$_ is the number that was just read, \$Lower is your lower bound of all numbers
So your lower limit and upper limit of a basket n would be the smallest and largest numbers, x and y, respectively, for which int(x - \$Lower)/\$Width = int(y - \$Lower)/\$Width = n

Thus, the lower limit will be (I don't need to prove I can ignore the int.. trust me, I can :))
n*width + \$Lower
and the upper limit will be
(n+1)*width - \$Lower - epsilon
(where epsilon is greater than the smallest number on your architecture such that, for any number z, z - epsilon does not equal z.... It looks like you've implicitly used 1.0)

0

LVL 5

Expert Comment

ID: 1206037
OK, so I'm using the word 'count', and you're using the word frequency. (BTW, I've assumed thoughout that your numbers are always positive, i.e. that \$Lower is > 0)

Also, it appears that I misunderstood your definitions of upper and lower, and that the min/max code I threw in there is uneccessary.

To figure out the limits, the salient piece of code is the

int(\$_ - \$Lower)/\$Width

\$_ is the number that was just read, \$Lower is your lower bound of all numbers
So your lower limit and upper limit of a basket n would be the smallest and largest numbers, x and y, respectively, for which int(x - \$Lower)/\$Width = int(y - \$Lower)/\$Width = n

Thus, the lower limit will be (I don't need to prove I can ignore the int.. trust me, I can :))
n*width + \$Lower
and the upper limit will be
(n+1)*width - \$Lower - epsilon
(where epsilon is greater than the smallest number on your architecture such that, for any number z, z - epsilon does not equal z.... It looks like you've implicitly used 1.0)

0

Author Comment

ID: 1206038
If I understood you well the program should work as it is.
but the count variable doesn't have a \$ in it as all perl variable so how to print it ?

What to print to get the follwing
class       Lower limit    upper limit  frequency    averagr
0           0.000           199.000      131        131/3
1           200.000         299.000       ?         ?
2           300.000         399.000       ?          ?
and so forth. Thanks

0

LVL 5

Expert Comment

ID: 1206039
You mean like

print "\$Buckets{4}->{count}\n";

0

Author Comment

ID: 1206040
Merci !
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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.…
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…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month11 days, 12 hours left to enroll

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

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