in reply to How to vector

sub vector { my ($aa) = @_; $aa->[0] = my $one; $aa->[1] = my $two; $aa->[2] = my $three; my $vector = V($one, $two, $three); }

In this function:

  1. The function is passed an array reference;
  2. The first three elements of the referent of this array reference are assigned the initial values (undef) of three newly-created lexical scalars;
  3. The values (all still undef) of these newly-created scalars are passed to the  Math::Vector::Real::V() function;
  4. The return value of the  V() call is assigned to a new lexical scalar  $vector (which immediately goes out of scope) and is also implicitly returned from the function call.
Does passing the scalar values of  $one $two $three (all undef) to  V() serve any purpose? Given that its argument parameters are always the same, can  V() ever return any differing value; i.e., is it an impure function?

I haven't examined Math::Vector::Real at all, but I would suspect things begin to go wrong within the  vector() call itself.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^2: How to vector
by jcklasseter (Sexton) on Jun 19, 2015 at 20:35 UTC
    Well, I was trying to have field 0,1,and 2 assigned to one, two, and three. Then have the vector assigned with this values as it's values. How could I change the array reference to each field value?
      How could I change the array reference to each field value?

      As I understand it, the array reference already references each field's value. You need to pass these values to V(). Maybe something like (untested):

      sub vector { my ($aa) = @_; return V(@$aa); }
      Again, I haven't studied the relevant module.

      As you can see, most of the function defined above is irrelevant. Why not just (also untested):

      my $some_scalar = V(@$original_array_reference);
      instead?

      Update: After actually looking at Math::Vector::Real, it seems that  V() returns a blessed reference of some kind (update: you also reference | make note of this below), not a floating point number as implied by the  %f format specifier in the OPed  printf statement. You will have to manipulate this  V object into some form of data usable to you, which I cannot judge.


      Give a man a fish:  <%-(-(-(-<