Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Help with arrays

by Kenosis (Priest)
on Sep 22, 2012 at 06:01 UTC ( #995050=note: print w/ replies, xml ) Need Help??


in reply to Help with arrays

What I am having trouble understanding is how to make an array with an uknown length?

It's common to have to work with an array without knowing its length--in Perl or other computer languages. Consider the following:

use strict; use warnings; my @array = qw/5 8 2 78 5 1 9 9 16/; # Example 1 for my $number (@array) { print $number, "\n"; } # Example 2 print "\n", 'Number of elements in @array: ', scalar @array; # Example 3 print "\n\n", 'Number of elements in @array: ', $#array + 1, "\n\n"; # Example 4 for ( my $i = 0 ; $i < @array ; $i++ ) { print $array[$i], "\n"; } # Example 5 my $total; for my $number (@array) { $total += $number; } print "\n", '$total is: ', $total;

First, notice the following at the top of the script:

use strict; use warnings;

Always have these, as they'll preemptively catch issues in your scripts, potentially saving your hours of headaches. (Omit them, however, if you prefer these headaches... :)

All the Examples could be working with an array of an unknown length. It just so happens that we know the length, since we've initialized it.

Example 1 iterates through each element of @array, assigning $number an each element's value that's then printed within the loop. Examples 2 & 3 show the number of elements in @array:

Number of elements in @array: 9

Example 4 shows a more traditional C-style for loop, and produces the same output as Example 1:

5 8 2 78 5 1 9 9 16

Example 5 shows one way to get the sum of the numbers in @array, and here's its output:

$total is: 133

Perhaps the above will assist you with how to "Find the average of the numbers in an array (@nums) of unknown length."

Hope this helps!


Comment on Re: Help with arrays
Select or Download Code
Re^2: Help with arrays
by perlguru22 (Acolyte) on Sep 22, 2012 at 06:27 UTC
    I think I Finally figured it out =)
    #!/usr/bin/perl use warnings; $sum = 0; @nums = (); push (@nums,(4,4)); $sum += $_ for @nums; $size = @nums; $average = $sum / $size; print "$average";
    This worked I am not sure if it's the correct way but it gave me the average

      Yes--well done!

      ...I am not sure if it's the correct way...

      Tim Toady!

      If I may, however, offer the following for you to consider:

      #!/usr/bin/perl use strict; use warnings; my $sum = 0; my @nums = (); push( @nums, ( 4, 8 ) ); $sum += $_ for @nums; my $size = @nums; my $average = $sum / $size; print "$average";

      My error...re-posted the above message.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-12-19 01:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls