Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Doubt

Expand Messages
  • Bharath G
    Hai, First I say Thank You for your reply, then I am not use MS Word, just i copy this text to ms file. Very much thank you for your reply. Your reply is
    Message 1 of 6 , Apr 20 1:59 AM
    • 0 Attachment
      Hai,

      First I say Thank You for your reply, then I am not use MS Word, just i copy this text to ms file. Very much thank you for your reply. Your reply is very useful for me.

      Thanks and Regards,

      D.G. Bharath




      Jenda Krynicky <Jenda@...> wrote: From: Bharath G
      > Hello Friends,
      >
      > I am Bharath, I am beginners for this language, I have a small doubt in this program,
      > line no 14 and 21 I am not understand any one explain this doubt for me

      You could have doubt in MS Word, meaning that you do not believe it
      works correctly. What you have in this case is a question.

      > 1: #!/usr/bin/perl -w
      > 2:
      > 3: $input = `´; # temporary input

      I believe it was your mail client, but there should be two
      singlequotes, not a backtick

      $input = '';

      Plus you should add

      use strict;

      on top of your script and declare the variables:

      my $input = '';

      > 4: @nums = (); # array of numbers;
      > 5: $count = 0; # count of numbers
      > 6: $sum = 0; # sum of numbers
      > 7: $avg = 0; # average
      > 8: $med = 0; # median
      > 9:
      > 10: while () {
      > 11: print `Enter a number: ´;
      > 12: chomp ($input = );
      > 13: if ($input ne `´) {
      > 14: $nums[$count] = $input;

      Perl arrays are indexed from zero. This means that if there are 5
      items in an array, then their indexes are 0, 1, 2, 3, 4. If you
      assign to index 5 you force the array to grow. That is after
      $array[5] = 99 you'll have an array with 6 items and the last one
      will be 99.

      Another thing that might confuse you is that you have an array @nums,
      but are assigning to $nums[$count]. This is because the @ or $ or %
      is not really part of the variable name, rather it specifies what the
      value of the expression is.

      @array
      is an array
      $array[3]
      is a scalar, that's why there is the $. It's THE third (counting from
      zero!) element of the array @array.


      Anyway in this case it's better to

      push @nums, $input;

      which means "add the $input at the end of the @nums array".

      > 15: $count++;

      and drop the line above

      > 16: $sum += $input;
      > 17: }
      > 18: else { last; }
      > 19: }

      It's preferable to write the loop like this

      while (1) {
      print 'Enter a number: ';
      chomp($input = );
      last if $input eq '';
      push @nums, $input;
      $sum += $input;
      }


      > 21: @nums = sort { $a <=> $b } @nums;

      This line uses the builtin function sort to sort the @nums array. The
      sort function accepts as it's first, optional parameter a block of
      code that defines the order. In this case a numerical, instead of the
      default text sort. ( (12,8,2) -> (2,8,12) instead of (12,2,8) )

      The block is used whenever the sort needs to compare two items. The
      two items are accessible as $a and $b and the block is supposed to
      return -1 if $a should be sorted before $b, 0 if the order doesn't
      matter and 1 if $b should be first.


      $count = scalar(@nums);
      the @nums array knows how many items it has so no need to keep
      incrementing the $count, just keep push()ing stuff to @nums and then
      get its size.

      > 22: $avg = $sum / $count;
      > 23: $med = $nums[$count /2];
      > 24:
      > 25: print "\nTotal count of numbers: $count\n";
      > 26: print "Total sum of numbers: $sum\n";
      > 27: print "Minimum number: $nums[0]\n";
      > 28: print "Maximum number: $nums[$#nums]\n";
      > 29: printf("Average (mean): %.2f\n", $avg);
      > 30: print "Median: $med\n";

      HTH, Jenda
      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery



      Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      Yahoo! Groups Links






      ---------------------------------
      Check out what you're missing if you're not on Yahoo! Messenger

      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.