Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Help with arrays

by perlguru22 (Acolyte)
on Sep 22, 2012 at 04:38 UTC ( #995037=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Help with arrays
in thread Help with arrays

I had this so far

#!/usr/bin/perl @nums = (); for(@nums) { $sum += $_; } $size = @nums; $average = $sum / $size; print "$average";
It works when I put elements in the @nums but I am just confused on the unknown length part.


Comment on Re^3: Help with arrays
Download Code
Re^4: Help with arrays
by 2teez (Priest) on Sep 22, 2012 at 04:53 UTC

    It works when I put elements in the @nums but I am just confused on the unknown length part.
    ofcourse, the array  @nums = () is empty.
    unknown length doesn't mean EMPTY array, does it?

    Infact, your code will die with an error message, illegal division by zero at ... because the variable $size will be 0 since the array is empty.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re^4: Help with arrays
by fluffyvoidwarrior (Monk) on Sep 22, 2012 at 05:01 UTC
    Perl is a pretty much untyped, dynamic language. When declaring an array in Perl you don't need to specify how big you intend it to be or what it contains. It will expand and shrink at runtime depending what you do with it.
    I imagine the problem is aimed at illustrating this and ensuring that you know how to handle it.
    So, all arrays are of unknown length unless you know what you put in them or you find out with:
    $size = @nums; or $last_index = $#nums
Re^4: Help with arrays
by Anonymous Monk on Sep 22, 2012 at 05:06 UTC

    Perl will handle array size (memory) as needed while populating an array. The "unknown length" part becomes crucial when you have to know the number of array elements beforehand if you were to write a C, C++, or Java program to fill an array. (In latter two languages there are other data structures -- vector, set, etc. -- to avoid the need to know the number of elements beforehand).

    An exercise of an array of "unknown length" would be to populate an array from a file of unknown lines (thus number of array elements) with each number listed on a line by itself.

      "An exercise of an array of "unknown length" would be to populate ..." -- self.

      That was rather sloppy, which should have been "A related exercise would be to populate ..." (among other variations).

Re^4: Help with arrays
by perlguru22 (Acolyte) on Sep 22, 2012 at 05:09 UTC
    @nums = (); for (0..$#nums)
    So would this be going in the right direction.
Re^4: Help with arrays
by smeezekitty (Novice) on Sep 22, 2012 at 05:29 UTC
    I think what you want is  $size = $#nums;

      I think what you want is $size = $#nums;
      Probably not. $#nums is the index of the last item in the array @nums. In the usual case, that is one less than scalar(@nums), which gives you the size of the @nums array, not the index of the last element.

      --MidLifeXis

Re^4: Help with arrays
by perlguru22 (Acolyte) on Sep 22, 2012 at 05:33 UTC
    #!/usr/bin/perl @nums = (); for(0..$#nums) { $sum += $_; } $size = $#nums; $average = $sum / $size; print "$average";
    This fix the illegal division of 0 and now it gives me the result of 0

      Anonymous Monk:
      An exercise of an array of "unknown length" would be to populate an array from a file of unknown lines (thus number of array elements) with each number listed on a line by itself.

      In the light of the above statement, let assume, your file with unknown length is given as below __DATA__, consider the codes below:

      use warnings; use strict; my $sum = 0; my @nums; # same as my @nums = (); while (<DATA>) { chomp; push @nums, $_; } $sum += $_ for @nums; my $size = @nums; my $average = $sum / $size; print $average, $/; __DATA__ 1 2 3 4 5 6

      NOTE: that @nums = () is assigning an empty list to the variable array @nums, so the array is empty. So, $size will be 0.

      You might what to check the following Up:
      push, pop, shift, unshift

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-30 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls