Perl sum of array and help with sorting

Perl sum of array and help with sorting

am 20.08.2007 21:44:12 von 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!

Re: Perl sum of array and help with sorting

am 20.08.2007 21:56:25 von Paul Lalli

On Aug 20, 3:44 pm, elroyerni wrote:
> 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

Re: Perl sum of array and help with sorting

am 20.08.2007 22:11:41 von jurgenex

elroyerni wrote:
> 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

Re: Perl sum of array and help with sorting

am 21.08.2007 02:47:01 von elroyerni

On Aug 20, 2:56 pm, Paul Lalli wrote:
> On Aug 20, 3:44 pm, elroyerni wrote:
>
>
>
> > 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

Thanks for your help, that simplified things tremendously!!

Re: Perl sum of array and help with sorting

am 21.08.2007 10:39:26 von Michele Dondi

On Mon, 20 Aug 2007 19:44:12 -0000, elroyerni
wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Perl sum of array and help with sorting

am 23.08.2007 15:49:05 von Ben Morrow

Quoth Michele Dondi :
> On Mon, 20 Aug 2007 19:44:12 -0000, elroyerni
> wrote:
>
> >sub sum_array {
> > my($sum) = 0; # initialize the sum to 0
> > foreach $i (@array_data) {
> > $sum = $sum + $i;
> > }
> > return($sum);
> >}


> 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

--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk
--
"Faith has you at a disadvantage, Buffy."
"'Cause I'm not crazy, or 'cause I don't kill people?"
"Both, actually."
[ben@morrow.me.uk]