Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > I don't understand loop count

Reply
Thread Tools

I don't understand loop count

 
 
BK
Guest
Posts: n/a
 
      02-17-2006
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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-17-2006
BK wrote:
> 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


 
Reply With Quote
 
 
 
 
MSG
Guest
Posts: n/a
 
      02-17-2006
BK wrote:
> 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.

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      02-17-2006
On 16 Feb 2006 18:53:32 -0800, "MSG" <(E-Mail Removed)> wrote:

>BK wrote:
>> 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 '*' ****ing mean anyway????
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-17-2006
robic0 wrote:
> 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 '*' ****ing mean
> anyway????


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

jue


 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      02-17-2006
robic0 wrote:


> quote: '*obvious*' ....
> Is this asterisk some sort of secret code I'm missing here?
> Just what in the **** does quoting words in '*' ****ing 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.


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      02-17-2006
Brian Wakem wrote:
> robic0 wrote:
>
>
>
>> quote: '*obvious*' ....
>>Is this asterisk some sort of secret code I'm missing here?
>>Just what in the **** does quoting words in '*' ****ing 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.


Mozilla 1.4.2 also does this.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      02-17-2006
Brian Wakem <(E-Mail Removed)> wrote:
> robic0 wrote:
>
>
> > quote: '*obvious*' ....
> > Is this asterisk some sort of secret code I'm missing here?
> > Just what in the **** does quoting words in '*' ****ing mean anyway????


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.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-17-2006
BK <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-17-2006
robic0 <> wrote:
> On 16 Feb 2006 18:53:32 -0800, "MSG" <(E-Mail Removed)> wrote:


>>Printing problems are the most *obvious* kind to debug.



> Is this asterisk some sort of secret code I'm missing here?



Yes.


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



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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Count bits in VHDL, with loop and unrolled loop produces different results a s VHDL 16 03-08-2011 05:35 PM
Re: Count bits in VHDL, with loop and unrolled loop producesdifferent results Gabor Sz VHDL 0 03-05-2011 03:28 AM
Re: Count bits in VHDL, with loop and unrolled loop producesdifferent results a s VHDL 2 03-04-2011 08:08 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM



Advertisments