Statis...:Vector(3pm)

πŸ“¦
version 1.6611 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Statistics::Basic::Vector β€” it's a whole class for handling lists of numbers, bestie πŸ’…


🌸 SYNOPSIS 🌸

okay so you invoke it like this, no cap:

my $vector      = vector(1,2,3);
my $same_vector = vector($vector);
my $different   = $vector->copy;

This module is lowkey a little stalker β€” it tracks which of the other Statistics::Basic modules are using it. That's literally its whole primary purpose. But also?? it has overloads so it can print your vectors all pretty πŸŽ€

print "$vector\n"; # pretty printed

πŸ’– METHODS πŸ’–

  • new() β€” the constructor, and she's flexible ✨ She can take a single array ref, or a single Statistics::Basic::Vector, or just a plain list of values. She returns a Statistics::Basic::Vector object.

Now here's the tea: if you hand her a vector object as the argument, she just returns that argument instead of making a new vector. The other Statistics::Basic modules do this to try to prevent duplicate calculations (iconic, honestly).

A vector's max size gets set to the size of the argument (or list) right at initialization.

Note: normally you'd use the vector() constructor rather than building these by hand with "new()". just saying πŸ‘€
  • copy() β€” makes a brand new vector object with the same contents and size as this one, and returns it.
my $v1 = vector(3,7,9);
my $v2 = $v1->copy(); # $v2 is a new object, separate vector
my $v3 = vector($v1); # $v3 is the same object as $v1
  • insert() β€” insert new values into the vector 🫢 If the vector was already full (check "set_size()"), this will also shift the oldest elements off the vector to compensate. balance, bestie.
$vector->insert( 4, 3 ); # insert a 3 and a 4

This function returns the object itself, for chaining purposes. so you can keep the party going πŸ’…

  • append() ginsert() β€” insert new values into the vector too! But if the vector was already full (see "set_size()"), these functions will grow the size of the vector to accommodate the new values, rather than shifting things out. "ginsert()" does the exact same thing, it's just an alias, they're twins fr fr.
$vector->append( 4, 3 ); # append a 3 and a 4

This function returns the object itself, for chaining purposes.

  • query() β€” returns the contents of the vector, either as a list or as an arrayref. context is everything, she's giving versatile queen πŸ‘‘
my @copy_of_contents      = $vector->query;
my $reference_to_contents = $vector->query;
⚠️ okay important: changing the $reference_to_contents will not usefully affect the contents of the vector itself β€” but it will adversely affect any computations based on the vector. if you need to change a vector's contents in a special way, use a Statistics::Basic::ComputedVector object instead, okay? don't do crimes πŸ™ˆ

Keeping $reference_to_contents around long term should work acceptably (since it refers to the vector contents itself).

  • query_filled() β€” returns true when the vector is the same size as the max size set by "set_size()". This function isn't useful unless you're operating under the effects of the nofill setting.
  • query_size() β€” returns the current number of elements in the vector object (NOT the size set with "set_size()", don't get it twisted). This is almost never false unless you're using the nofill setting.
  • set_size() β€” sets the max size of the vector.
my $v1 = vector(1,2,3);
   $v1->set_size(7); # [0, 0, 0, 0, 1, 2, 3]

Unless nofill is set, the vector will be filled with 0s (assuming the vector wouldn't otherwise be full) on the oldest side of the vector β€” so an insert will push off one of the filled-zeros. slay πŸ’«

This function returns the object itself, for chaining purposes.

my $v1 = vector(2 .. 5)->set_size(5);
# [0, 2, 3, 4, 5]
  • set_vector() β€” give it a vector or an array ref, and it'll set the contents (and size) of the input vector to match that argument. And if you give it a vector object argument, this makes the two vectors match while still remaining separate objects β€” like matching outfits but separate people, cute πŸ¦‹
my $v1 = vector(3,7,9);
my $v2 = vector()->set_vector($v1);
my $v3 = vector($v1); # $v3 is the same object as $v1

This function returns the object itself, for chaining purposes.


🌈 OVERLOADS 🌈

This object is overloaded. She tries to return an appropriate string for the vector, and raises errors in numeric context (she said don't do math on me like that πŸ’€).

In boolean context, this object is always true (even when empty). main character energy, she's always showing up ✨


πŸ‘‘ AUTHOR πŸ‘‘

Paul Miller "<jettero@cpan.org>"


Copyright 2012 Paul Miller -- Licensed under the LGPL


🫢 SEE ALSO 🫢


and that's the whole page bestie!! this was perl v5.36.0, dated 2022-11-19, from the Statis...:Vector(3pm) files πŸ’œ go forth and vector responsibly, ily, mwah 🧚