I don't understand loop count

Discussion in 'Perl Misc' started by BK, Feb 17, 2006.

  1. BK

    BK Guest

    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
     
    BK, Feb 17, 2006
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Feb 17, 2006
    #2
    1. Advertising

  3. BK

    MSG Guest

    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.
     
    MSG, Feb 17, 2006
    #3
  4. BK

    robic0 Guest

    On 16 Feb 2006 18:53:32 -0800, "MSG" <> 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 '*' fucking mean anyway????
     
    robic0, Feb 17, 2006
    #4
  5. 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 '*' fucking mean
    > anyway????


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

    jue
     
    Jürgen Exner, Feb 17, 2006
    #5
  6. BK

    Brian Wakem Guest

    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.


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Feb 17, 2006
    #6
  7. 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 '*' 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.


    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
     
    Josef Moellers, Feb 17, 2006
    #7
  8. BK

    Dave Weaver Guest

    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 '*' fucking 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.
     
    Dave Weaver, Feb 17, 2006
    #8
  9. BK

    Anno Siegel Guest

    BK <> 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.
     
    Anno Siegel, Feb 17, 2006
    #9
  10. robic0 <> wrote:
    > On 16 Feb 2006 18:53:32 -0800, "MSG" <> 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 '*' fucking mean anyway????



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 17, 2006
    #10
  11. BK

    robic0 Guest

    On 17 Feb 2006 10:13:43 GMT, Dave Weaver <> wrote:

    >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 '*' fucking 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.


    *_/Thanks alot!/_*
     
    robic0, Feb 22, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. efelnavarro09
    Replies:
    2
    Views:
    953
    efelnavarro09
    Jan 26, 2011
  2. a s
    Replies:
    16
    Views:
    4,684
    JustJohn
    Mar 8, 2011
  3. a s
    Replies:
    2
    Views:
    1,716
    JustJohn
    Mar 4, 2011
  4. Gabor Sz
    Replies:
    0
    Views:
    867
    Gabor Sz
    Mar 5, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page