bjohn has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a program that will ask the user for values, one value per line, until they enter a blank line. then output the min, the max, and the average of the values.

Replies are listed 'Best First'.
Re: beginner needs help
by wind (Priest) on Mar 22, 2011 at 01:05 UTC

    Yep, that's a pretty common homework problem. In fact, someone else had such homework about a week ago. What have you tried thus far?

    For a cpan module, look at List::Util

    Might help if you read this: How do I post a question effectively?.

Re: beginner needs help
by toolic (Bishop) on Mar 22, 2011 at 01:09 UTC
Re: beginner needs help
by ikegami (Patriarch) on Mar 22, 2011 at 01:09 UTC
    You seem to have forgotten to ask a question.
Re: beginner needs help
by bjohn (Initiate) on Mar 22, 2011 at 01:23 UTC
    yea i c that im not asking the question correctly sry. ill figure it out. its just that the instructor doesn't explain things very well and im having issues. sry

      Steps toward getting a useful answer:

      • Start by reading the course materials, if any.
      • Try to get started with it.
      • If you run into a roadblock, ask the instructor about it.
      • If (s)he's not able to explain clearly enough to help, post here what you've done so far and ask a specific question on how to proceed.

      It will be ineffective to just ask people to do your homework for you. But the primary reason for the ineffectiveness isn't that people here aren't willing to help. It's that us doing your homework is ineffective at helping you to master the subject matter. Since we're generally aware of that fact, and since this site is geared toward helping people in their quest for knowledge (as opposed to their need to turn in assignments on time), we're often hesitant to just provide the answer when there's little apparent effort. That's just not what we're all about here.

      In the case of your problem, I would do this: I would create four variables. One to hold the lowest value entered. One to hold the highest value entered. One to hold the tally of all values entered. And one to keep track of how many values have been entered. I would initially set all the variables to hold 'undef'. Then I would start a loop. I would query for input. I would test for a blank line. Then I would chomp, test for min-ness and max-ness (setting a new min or max if necessary), I would add the input value to the tally, and I would increment the sample count. If I wanted to play it safe I would also reject any input that was non-numeric. After the loop ends I would take care of the math and print the output. Often if you think through the steps, the code becomes easy.


      Dave

        You can check whether the input is numeric by looking into perl's regular expressions. Hope this helps..

        first of all i was not asking u all to do my hw although it may hv appeared that way, sorry. one of my friends told me about this site and i wanted to c if it was for real because our instructor goes very fast and is not always open to questions and i need help some times. so i will come back as i run into issues. thanks guys
      Step 0 when programming, whether in the classroom, or later when programming in exchange for money: if you don't understand what your instructor/boss/client wants, ask said person, not an unrelated third party.
      If there's something that you need explained, feel free to ask for an explanation here and you'll likely get at least one good explanation1. Nobody has a problem with that.

      What people are having an issue with is that you've basically said "this is my homework assignment", but not asked any particular question, which gives the impression that you want us to just do your homework for you. We don't do that around here, since it won't actually help you in the long run if we do.

      So, what do you need explained? If it's a conceptual issue, just ask about it and we can explain it. If it's a specific problem with your code, post the code and explain the problem you want help with and we can show you how to do it correctly.


      1 It's quite possible that you may get several good explanations, some of which will be very precise about technical details that go over your head at this point, but at least one should be appropriate for a beginner.