I don't understand loop count

B

BK

Hi

I have the following code in which I'm trying to count some numbers in
a given range; Problem is when print is called, it doesn't give me the
total number of counts, however, prints each count separately....

#!c:\perl\bin\perl.exe -w

print "Please enter measurements, separated by commas: ";
$data = <STDIN>; chomp $data;
@data = split(",", $data);

@data = sort {$a <=> $b} @data;

print "Measurements to be analysed: @data\n";

$a = 0; $b = 0;

foreach $data (@data) {

$data =~ m/[0-9]+(.*)/; chomp ($data);

if($data <= 10){($a)++; print ("Count below 10: $a\n");}
if($data >= 11 && $data <= 20) { ($b)++; print ("Count btwn 11 and 20:
$b\n");}



}

Thanking you in advance for your help
 
J

Jürgen Exner

BK said:
I have the following code in which I'm trying to count some numbers in
a given range; Problem is when print is called, it doesn't give me the
total number of counts, however, prints each count separately....

#!c:\perl\bin\perl.exe -w
Better to use
use warnings;
You are missing
use strict;
print "Please enter measurements, separated by commas: ";
$data = <STDIN>; chomp $data;
@data = split(",", $data);

@data = sort {$a <=> $b} @data;

I have no idea why you are sorting your data because in the further code
below the sequence doesn't/shouldn't matter.
print "Measurements to be analysed: @data\n";

$a = 0; $b = 0;

Poor use of variable names. $a and $b have special meaning in sort() and
therefore should not be used outside of a sort comparison.
foreach $data (@data) {

This is a poor choice for a variable name because you already have a $data
containing some totally different content.
$data =~ m/[0-9]+(.*)/; chomp ($data);

Why this line? You match something but you never do anything based on the
match results. And why the chomp() again? You already chomp()ed your input
way up.
if($data <= 10){($a)++; print ("Count below 10: $a\n");}
if($data >= 11 && $data <= 20) { ($b)++; print ("Count btwn 11 and 20:
$b\n");}
}

Not sure what you mean by
"doesn't give me the total number of counts, however, prints each count
separately...."
Your print statements are inside the loop, so yes, for each iteration of
course you will print the total that was added up so far.
Do you want something different?

jue
 
M

MSG

BK said:
Hi

I have the following code in which I'm trying to count some numbers in
a given range; Problem is when print is called, it doesn't give me the
total number of counts, however, prints each count separately....

#!c:\perl\bin\perl.exe -w

print "Please enter measurements, separated by commas: ";
$data = <STDIN>; chomp $data;
@data = split(",", $data);

@data = sort {$a <=> $b} @data;

print "Measurements to be analysed: @data\n";

$a = 0; $b = 0;

foreach $data (@data) {

$data =~ m/[0-9]+(.*)/; chomp ($data);

if($data <= 10){($a)++; print ("Count below 10: $a\n");}
if($data >= 11 && $data <= 20) { ($b)++; print ("Count btwn 11 and 20:
$b\n");}
}

Printing problems are the most *obvious* kind to debug. You
should be able to figure it out yourself w/o much effort.
(Hint: you are printing at each interation of the foreach loop!)

People have previously advised you about
use strict;
You should seriously consider doing it ASAP!

The regex line in your code is useless. You should almost always
use it in a while( ) or if( ).

$a, $b are very special variables with sort. Casually using them in
your code could burn you sooner or later.
 
R

robic0

BK said:
Hi

I have the following code in which I'm trying to count some numbers in
a given range; Problem is when print is called, it doesn't give me the
total number of counts, however, prints each count separately....

#!c:\perl\bin\perl.exe -w

print "Please enter measurements, separated by commas: ";
$data = <STDIN>; chomp $data;
@data = split(",", $data);

@data = sort {$a <=> $b} @data;

print "Measurements to be analysed: @data\n";

$a = 0; $b = 0;

foreach $data (@data) {

$data =~ m/[0-9]+(.*)/; chomp ($data);

if($data <= 10){($a)++; print ("Count below 10: $a\n");}
if($data >= 11 && $data <= 20) { ($b)++; print ("Count btwn 11 and 20:
$b\n");}
}

Printing problems are the most *obvious* kind to debug. You
should be able to figure it out yourself w/o much effort.
(Hint: you are printing at each interation of the foreach loop!)

People have previously advised you about
use strict;
You should seriously consider doing it ASAP!

The regex line in your code is useless. You should almost always
use it in a while( ) or if( ).

$a, $b are very special variables with sort. Casually using them in
your code could burn you sooner or later.
quote: '*obvious*' ....
Is this asterisk some sort of secret code I'm missing here?
Just what in the **** does quoting words in '*' fucking mean anyway????
 
J

Jürgen Exner

robic0 said:
Is this asterisk some sort of secret code I'm missing here?

Code? Kind of. Secret? Not at all. Other people use the underscore in the
same place to express the same.
Just what in the **** does quoting words in '*' fucking mean
anyway????

No, it has nothing to do with the activity that you mentioned.

jue
 
B

Brian Wakem

robic0 wrote:

quote: '*obvious*' ....
Is this asterisk some sort of secret code I'm missing here?
Just what in the **** does quoting words in '*' fucking mean anyway????


In many newsreaders (including knode, which I use) putting something in
*asterisks* highlights it. _underscores_ underlines and /slashes/
italicise. There may be others I'm not aware of.
 
J

Josef Moellers

Brian said:
robic0 wrote:






In many newsreaders (including knode, which I use) putting something in
*asterisks* highlights it. _underscores_ underlines and /slashes/
italicise. There may be others I'm not aware of.

Mozilla 1.4.2 also does this.
 
D

Dave Weaver

Brian Wakem said:
robic0 wrote:

Well *I* thought it was obvious.
It's simply a way of indicating stress on a word.
And much more civilized than inserting 4-letter words, a practice that just
encourages people to ignore you.
In many newsreaders (including knode, which I use) putting something in
*asterisks* highlights it. _underscores_ underlines and /slashes/
italicise. There may be others I'm not aware of.

And slrn colours the words to highlight them.
 
A

Anno Siegel

BK said:
Hi

I have the following code in which I'm trying to count some numbers in
a given range; Problem is when print is called, it doesn't give me the
total number of counts, however, prints each count separately....

#!c:\perl\bin\perl.exe -w

print "Please enter measurements, separated by commas: ";
$data = <STDIN>; chomp $data;
@data = split(",", $data);

@data = sort {$a <=> $b} @data;

print "Measurements to be analysed: @data\n";

$a = 0; $b = 0;

foreach $data (@data) {

$data =~ m/[0-9]+(.*)/; chomp ($data);

if($data <= 10){($a)++; print ("Count below 10: $a\n");}
if($data >= 11 && $data <= 20) { ($b)++; print ("Count btwn 11 and 20:
$b\n");}

Your print statements are inside the counting loop, so it prints
every intermediate value. What did you expect?

Counting things that meet a criterion can be done with grep in scalar
context (untested):

chomp @data; # chomps all list elements
my $low_count = grep $_ <= 10, @data;
my $high_count = grep 11 <= $_ and $_ <= 20, @data;

Anno
 
T

Tad McClellan

robic0 said:
Is this asterisk some sort of secret code I'm missing here?

Yes.


Just what in the **** does quoting words in '*' fucking mean anyway????


If we told you, then it wouldn't be *secret* anymore now would it?
 
R

robic0

Well *I* thought it was obvious.
It's simply a way of indicating stress on a word.
And much more civilized than inserting 4-letter words, a practice that just
encourages people to ignore you.


And slrn colours the words to highlight them.

*_/Thanks alot!/_*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top