Perl sum of array and help with sorting

E

elroyerni

Hi -

I have a array of a list of numbers:

7.9216
8.7583
12.675
0.8028
6.9230
1.1403
6.0083
0.1454

I wrote a sub-routine to add the list, but i'm getting these syntax
errors, and was wondering if someone could tell me what i'm doing
wrong. here's my code for the sub-routine:
sub sum_array {
my($sum) = 0; # initialize the sum to 0
foreach $i (@array_data) {
$sum = $sum + $i;
}
return($sum);
}

It's returning this error: isn't numeric in addition (+)

Also I'm trying to write a sub-routine that will go through each
element in the array and tell me how many elements in the array are
less than a given value. For example in the array above say i want to
return the amount of elements that are less than 5 seconds. From the
array above I'd return 3.Here's what I have so far:
sub less_five {
foreach $r(@array_data){
count=0;
while ($count<5)
{
$count++;
}
}
return($data);
}

Cant seem to get this to work.. Thanks for your help!
 
P

Paul Lalli

Hi -

I have a array of a list of numbers:

7.9216
8.7583
12.675
0.8028
6.9230
1.1403
6.0083
0.1454

I wrote a sub-routine to add the list, but i'm getting these syntax
errors, and was wondering if someone could tell me what i'm doing
wrong. here's my code for the sub-routine:
sub sum_array {
my($sum) = 0; # initialize the sum to 0
foreach $i (@array_data) {
$sum = $sum + $i;
}
return($sum);

}

It's returning this error: isn't numeric in addition (+)

Okay, first of all, that's a warning, not an error. In any event,
basically this means your array's contents aren't what you think they
are. You have at least one non-numeric element in that array. To see
exactly what's in it, use these lines:

use Data::Dumper;
print Dumper(\@array_data);

Also I'm trying to write a sub-routine that will go through each
element in the array and tell me how many elements in the array are
less than a given value. For example in the array above say i want to
return the amount of elements that are less than 5 seconds. From the
array above I'd return 3.Here's what I have so far:
sub less_five {
foreach $r(@array_data){
count=0;
while ($count<5)
{
$count++;

}
}
return($data);
}

Cant seem to get this to work.

Your logic on this one mystifies me. I don't at all understand what
the while loop is supposed to do, nor do I understand where $data came
from. This is a lot simpler than you're making it:

sub less_five {
my $count;
foreach my $r (@array_data) {
if ($r < 5) {
$count++;
}
}
return $count;
}

or, much more simply:

my $count = grep { $_ < 5 } @array_data;

Paul Lalli
 
J

Jürgen Exner

elroyerni said:
I have a array of a list of numbers:

7.9216
8.7583
12.675
0.8028
6.9230
1.1403
6.0083
0.1454

I wrote a sub-routine to add the list, but i'm getting these syntax
errors, and was wondering if someone could tell me what i'm doing
wrong. here's my code for the sub-routine:
sub sum_array {
my($sum) = 0; # initialize the sum to 0
foreach $i (@array_data) {
$sum = $sum + $i;
}
return($sum);
}

It's returning this error: isn't numeric in addition (+)

I cannot repro this behaviour. Using your code and sample data I am getting
a clear 44.3747 as return value. Can you please post a _COMPLETE_ script
that demonstrates this message, such that we can copy and run the script?

On a side note: a more perlish way would be
foreach (@array_data) {
$sum += $_;

An even simpler way is to use sum() from List::Util
return sum(@array_data);
Also I'm trying to write a sub-routine that will go through each
element in the array and tell me how many elements in the array are
less than a given value.

That is a typical application for grep().
For example in the array above say i want to
return the amount of elements that are less than 5 seconds. From the
array above I'd return 3.

sub less_five {
return scalar grep ($_ < 5, @array_data);
}
Here's what I have so far:
sub less_five {
foreach $r(@array_data){
count=0;

You are resetting your counter to 0 for each element of the array.
while ($count<5)
{
$count++;

Why do you loop to increment the counter until the value of the counter is
larger than 5? That doesn't make any sense to me.
Maybe you meant
if ($r < 5) {$count++}
instead?

jue
 
E

elroyerni

Okay, first of all, that's a warning, not an error. In any event,
basically this means your array's contents aren't what you think they
are. You have at least one non-numeric element in that array. To see
exactly what's in it, use these lines:

use Data::Dumper;
print Dumper(\@array_data);




Your logic on this one mystifies me. I don't at all understand what
the while loop is supposed to do, nor do I understand where $data came
from. This is a lot simpler than you're making it:

sub less_five {
my $count;
foreach my $r (@array_data) {
if ($r < 5) {
$count++;
}
}
return $count;

}

or, much more simply:

my $count = grep { $_ < 5 } @array_data;

Paul Lalli

Thanks for your help, that simplified things tremendously!!
 
M

Michele Dondi

sub sum_array {
my($sum) = 0; # initialize the sum to 0
foreach $i (@array_data) {
$sum = $sum + $i;
}
return($sum);
}

In addition to all that's been told to you, it's also worth to notice
that you're writing a separate sub to sum a *specific* array. This is
*legitimate*, but most probably *not* the best thing to do. Taking
your sub as it is, just modify it like

sub sum_array {
my($sum) = 0; # initialize the sum to 0
foreach $i (@_) {
$sum = $sum + $i;
}
return($sum);
}

so that you can call it like thus:

my $sum1 = sum_array(@array1);
my $sum2 = sum_array(@array2);
# and so on...

Of course when you've modified it this way you can sum not only
arrays, but arbitrary lists:

my $sum = sum_array(1,2.5,3);

At this point the name is misleading at best, and I would probably go
for just 'sum'. BTW: a completely equivalent but more perlish way in
which probably most Perl programmers would write it is

sub sum {
my $sum;
$sum += $_ for @_;
$sum;
}


Michele
 
B

Ben Morrow

At this point the name is misleading at best, and I would probably go
for just 'sum'. BTW: a completely equivalent but more perlish way in
which probably most Perl programmers would write it is

sub sum {
my $sum;
$sum += $_ for @_;
$sum;
}

Or

use List::Util qw/sum/;

:)

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top