How to plot a histogram from an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jangiwan
    New Member
    • Mar 2008
    • 3

    How to plot a histogram from an array

    Dear All,

    I am new to perl. I need your help with processing the grade.dat file. I have a file containing a list of students with their varying scores from different type of assignments and tests. I have calculated the final grade and need to know the score distribution in a simple histogram like this:
    78 xxxxxxx
    79 xxxxxxxx
    80 xxxxx
    81 xxx
    where x indicates the number of students gaining the score that equals to the left string.
    Thanks for your prompt response.

    Regards,
    Jang
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Please show us the code that you have done in tying to accomplish this on your own. Also, this sound like homework/coursework, is it?

    --Kevin

    Comment

    • jangiwan
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by eWish
      Please show us the code that you have done in tying to accomplish this on your own. Also, this sound like homework/coursework, is it?

      --Kevin
      Yes, I've been working on my assignment (If this is not allowed, then I won't be asking questions anymore).
      Well, to be frank, I haven't been able to compute the final score since I'm still trying to find a way to target a specific column. For example, the mid-test scores are at column 20 and final-test scores at 21 and I have to add them in to get the overall score of each student. Below is what I had in mind:

      [CODE=perl]$mid = $i;
      for ($i = 20) {
      $overall = $overall + $mid;
      }
      $final = $i;
      for ($i = 21) {
      $overall = $overall + $final;
      }[/CODE]

      Are they correct? Anyone willing to help?
      Thanks.

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        Would you please show some sample data and the format of the file that the data is in?

        When it comes to school work we will make suggestions, but not offer complete solutions. That way you will still learn.

        --Kevin

        Comment

        • jangiwan
          New Member
          • Mar 2008
          • 3

          #5
          Originally posted by eWish
          Would you please show some sample data and the format of the file that the data is in?

          When it comes to school work we will make suggestions, but not offer complete solutions. That way you will still learn.

          --Kevin
          00569, Cindy, 40, 40, 40 ,38, 40, 40, 20, 25, 25, 10, 8, 9, 8, 8, 7, 8, 66, 82, 92
          00580, Melina, 40, 39, 38, 40, 37, 40, 25, 25, 25, 10, 9, 9, 9, 8, 9, 70, 83, 93

          The data contain the ID, name, scores of projects, quizzes, exercises, mid and final terms. I have to add up all scores for every student by first counting the percentage of each element of scoring and then assign a letter grade like A, B, and etc. Then i have to display a histogram like this.

          90 xxxx
          91 xxx
          92 x
          93 x
          which means four students got 90, 3 got 91 and so on.
          I'd really appreciate you would give me some clue.
          Thanks.

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            [CODE=perl]# This gets the last two elements the line.
            my ($mid, $final) = (split ', ')[-2,-1];

            # This assumes that each line is the same fixed length.
            my ($mid, $final) = (split', ',)[19, 20];[/CODE]
            [CODE=perl]
            open(my $FILE, '<', $file) || die "Can't open $file: $!\n";
            while(<$FILE>) {
            chomp;

            # This gets the last two elements the line.
            my ($mid, $final) = (split ', ')[-2,-1];

            # Do something with $mid and $final
            print "Mid: $mid\n";
            print "Final: $final\n";

            }
            close($FILE);[/CODE]

            If you store each occurrence of the various grades in a hash you can access them like so.

            [CODE=perl]
            # $keys are the grade and $values would be the number of occurrences.
            while (my ($keys, $values) = each %scores_count) {
            print "$keys ", ('X' x $values), "\n";
            }[/CODE]

            --Kevin

            Comment

            Working...