Ciao has asked for the wisdom of the Perl Monks concerning the following question:

SOLVED!

I'm new to arrays. You folks helped me the other day with a different portion of this library and now I need to take the array from <STDIN>, divide each element by 2 and return the values. I'm getting the error:
Can't modify private array in division (/) at lib.pl line 31, near "2;"


Updated code
#!/usr/bin/perl use strict; use warnings; no warnings 'numeric'; my $line; sub calcsum { my @funcArray = @_; my $funcSum = 0; my $arrcount = scalar(@_); foreach my $line (@funcArray) { $funcSum += $line; } return $funcSum; } sub calcavg { my @funcArray = @_; my $arrcount = scalar(@_); my $funcAvg = 0; foreach my $line (@funcArray) { $funcAvg += $line; } return ($funcAvg / $arrcount); } sub calcarr { my @funcArray = @_; my @funcArr = (); foreach my $line (@funcArray) { $line/= 2; push(@funcArr,$line); } return @funcArr; } 1;

The trouble is with the last subroutine. I also tried the operation without the = but got the following error:
Useless use of division (/) in void context at obj13-lib.pl line 31.

Here is the main script

Updated code
#!/usr/bin/perl use strict; use warnings; no warnings 'numeric'; require 'obj13-lib.pl'; my @userArray = <STDIN>; my @arr = calcarr(@userArray); my $sum = calcsum(@userArray); my $avg = calcavg(@userArray); print "Sum $sum\n"; print "Avg $avg\n"; print "New Array $arr\n";

Thank you!

Replies are listed 'Best First'.
Re: Can't modify private array in division (/) at lib.pl line 31, near "2;"
by jethro (Monsignor) on Apr 09, 2009 at 00:13 UTC

    Here is a corrected version of your loop in calcarr. You might like to reread a chapter about perl arrays because it looks like you have some wrong notions about them

    # my @funcArr = 0; my @funcArr= (); #this is how an array can be initialized to be empt +y foreach my $line (@funcArray) { # @funcArray /= 2; $line/= 2; #$line is aliased to each value of the array in the +loop push(@funcArr,$line); }
      my @funcArr= (); #this is how an array can be initialized to be empty

      That line of code does nothing at runtime, except take up time doing nothing. Arrays start out initialized as empty.

Re: Can't modify private array in division (/) at lib.pl line 31, near "2;"
by JavaFan (Canon) on Apr 08, 2009 at 23:51 UTC
    You cannot divide arrays by 2, which is what you are doing in calcarr. Perhaps you want:
    sub calcarr {map {$_ / 2} @_} my @arr = calcarr(@funcArr);
    Note the @arr instead of $arr. But I've no idea what @funcArr is supposed to be - your main script uses it, but never assigns to it. In fact, I don't think your main script even compiles.

      You cannot divide arrays by 2

      I understand what you meant, but to be really pedantic ... I think you can divide them by two, but you can't assign it back... $x = @a/2 fine, @x = 2 not so much... and that's what @a /= 2 is doing, but it's doing it to a protected value, which is the size of the array.

      UPDATE: I understand that it's not what he wants and that your answer is essentially correct. I was correcting the very small pedantic point that you can divide an array by 2, you just can't set its size to 2 with @a/=2. I definitely wasn't clear on what I meant either though. @x=2 is fine, but it's really @x=(2). You can't actually express what @a/=2 is assigning, which is @a.size = @a/2. Looks like ruby or something -- you could express it with splice, but you'd have to (at least implicitly) mention the start and end of your size reduction.

      -Paul

        @a / 2 gives you half the number of elements in @a. Which isn't even close to what the OP wants to achieve. You can BTW assign such a result back:
        $ perl -wE '@a = "a" .. "z"; @a = @a / 2; say @a' 13
        But @a /= 2 will fail. However, it has little to do what the OP wants.
      @funcArr is supposed to be the "NEW" array containing all the values of the old array divided by 2.
      Ciao