GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Tie::Hash::Indexed(3) User Contributed Perl Documentation Tie::Hash::Indexed(3)

Tie::Hash::Indexed - Ordered hashes for Perl

  use Tie::Hash::Indexed;

  # Object Oriented Interface
  my $hash = Tie::Hash::Indexed->new(
               I => 1, n => 2, d => 3, e => 4);
  $hash->push(x => 5);

  print $hash->keys, "\n";   # prints 'Index'
  print $hash->values, "\n"; # prints '12345'

  # Tied Interface
  tie my %hash, 'Tie::Hash::Indexed';

  %hash = ( I => 1, n => 2, d => 3, e => 4 );
  $hash{x} = 5;

  print keys %hash, "\n";    # prints 'Index'
  print values %hash, "\n";  # prints '12345'

Tie::Hash::Indexed is intentionally very similar to other ordered hash modules, most prominently Hash::Ordered. However, Tie::Hash::Indexed is written completely in XS and is, often significantly, faster than other modules. For a lot of operations, it's more than twice as fast as Hash::Ordered, especially when using the object-oriented interface instead of the tied interface. Other modules, for example Tie::IxHash, are even slower.

The object-oriented interface of Tie::Hash::Indexed is almost identical to that of Hash::Ordered, so in most cases you should be able to easily replace one with the other.

If you don't need the last bit of performance and feel more comfortable with a pure-Perl module, Hash::Ordered is definitely a good alternative.

Tie::Hash::Indexed should build with perl versions as old as 5.005. It should build on any platform if a C compiler is available.

Tie::Hash::Indexed has no "clone" method, but cloning can be emulated with:

  $clone = Tie::Hash::Indexed->new($orig->items);

Tie::Hash::Indexed has an "items" method as an alias for "as_list", which Hash::Ordered lacks. If you want to be able to switch modules, you should prefer to use "as_list".

Tie::Hash::Indexed also has a "has" method as an alias for "exists".

Tie::Hash::Indexed also has "dor_assign" and "or_assign" as aliases for "dor_equals" and "or_equals".

Tie::Hash::Indexed has "multiply", "divide" and "modulo" methods in addition to "add" and "subtract". Hash::Ordered only supports "add" and "subtract".

Tie::Hash::Indexed has an "assign" method that can be used to directly assign a new list of key-value pairs to an existing instance. With Hash::Ordered, you can call "clear" followed by "merge" to get the same behaviour.

Tie::Hash::Indexed has a "reverse_iterator" method, which can be emulated in Hash::Ordered by passing the reversed list of keys to "iterator". On the other hand, the "iterator" method of Tie::Hash::Indexed doesn't support passing in a list of keys at all.

Tie::Hash::Indexed objects always evaluate to a true value in boolean context, unlike Hash::Ordered object, which evaluate to a false value if they are empty, and a true value otherwise. You can use "$obj-"keys> with Tie::Hash::Indexed instead, which is extremely cheap to call in scalar context.

  $obj = Tie::Hash::Indexed->new;
  $obj = Tie::Hash::Indexed->new(@kvpairs);

Construct and optionally initialize a new object.

  $obj->clear;

Removes all contents from the hash. Returns the object, which allows for method chaining.

Invalidates iterators.

  $obj->assign(@kvpairs);

Clears the hash and assigns the list of key-value pairs. Identical to:

  $obj->clear->merge(@kvpairs);

Returns the number of keys stored in the hash after assigning.

Invalidates iterators.

  $obj->merge(@kvpairs);

Merge a lists of key-value pairs into the hash. Existing keys will remain in their position and have their value updated. New keys will be appended to the end.

Returns the number of keys stored in the hash after merging.

Invalidates iterators.

  $bool = $obj->exists($key)

Returns a boolean indicating if a key exists in the hash.

An alias for "exists".

  $value = $obj->get($key)

Returns the value for a single key, or "undef" if the key was not found.

  $obj->set($key, $value)

If the key already exists, update the value without affecting the item order. Otherwise append the key-value pair. This is equivalent to calling "merge" with a single key-value pair, except for the return value.

Returns the value.

Invalidates iterators.

  $obj->push(@kvpairs)

Push one or more key-value pairs. This is similar to "merge", but instead of preserving the position of existing keys, this will remove existing keys and append all key-value pairs to the end.

Returns the number of keys stored in the hash after pushing.

Invalidates iterators.

  $obj->unshift(@kvpairs)

Pushes one or more key-value pairs to the start. This is similar to "push", but operates on the start of the ordered hash. Existing keys will be removed and inserted at the start.

Returns the number of keys stored in the hash after unshifting.

Invalidates iterators.

  $value = $obj->pop;
  ($key, $value) = $obj->pop;

Removes the last item from the ordered hash.

Returns the value in scalar context or the key-value pair in list context.

Invalidates iterators.

  $value = $obj->shift;
  ($key, $value) = $obj->shift;

Removes the first item from the ordered hash.

Returns the value in scalar context or the key-value pair in list context.

Invalidates iterators.

  $value = $obj->delete($key);

Removes a key-value pair from the ordered hash and returns the value.

Invalidates iterators if the key was found.

  @kvpairs = $obj->items;
  @kvpairs = $obj->items(@keys);

Returns the key-value pairs for all items in the hash, or just for the selected keys. In scalar context, returns the number of list elements that would be returned in list context.

If a key is not found, the associated value will be returned as "undef".

An alias for "items".

  @keys = $obj->keys;
  @keys = $obj->keys(@keys);

Returns the keys for all items in the hash, or just for the selected keys. In scalar context, returns the number of list elements that would be returned in list context.

  @values = $obj->values;
  @values = $obj->values(@keys);

Returns the values for all items in the hash, or just for the selected keys. In scalar context, returns the number of list elements that would be returned in list context.

If a key is not found, the associated value will be returned as "undef".

  $obj->concat($key, $str);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) . $str);

  $obj->add($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) + $value);

  $obj->subtract($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) - $value);

  $obj->multiply($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) * $value);

  $obj->divide($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) / $value);

  $obj->modulo($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) % $value);

  $obj->dor_assign($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) // $value);

This is an alias for "dor_assign".

  $obj->or_assign($key, $value);

This is equivalent to, but more efficient than:

  $obj->set($key, $obj->get($key) || $value);

This is an alias for "or_assign".

  $val = $obj->postinc($key);

This is equivalent to, but more efficient than:

  $val = $obj->get($key);
  $obj->set($key, $val + 1);

  $val = $obj->postdec($key);

This is equivalent to, but more efficient than:

  $val = $obj->get($key);
  $obj->set($key, $val - 1);

  $val = $obj->preinc($key);

This is equivalent to, but more efficient than:

  $val = $obj->set($key, $obj->get($key) + 1);

  $val = $obj->predec($key);

This is equivalent to, but more efficient than:

  $val = $obj->set($key, $obj->get($key) - 1);

  my $i = $h->iterator;
  while (my($k, $v) = $i->next) {
    push @key, $k;
    push @val, $v;
  }

Bidirectional forward iterator for ordered hash traversal.

  for (my $i = $h->reverse_iterator; $i->valid; $i->next) {
    push @key, $i->key;
    push @val, $i->value;
  }

Bidirectional reverse iterator for ordered hash traversal.

If Tie::Hash::Indexed is built with debugging support, you can use this environment variable to specify debugging options. Currently, the only useful values you can pass in are "d" or "all", which both enable debug output for the module.

As the data of Tie::Hash::Indexed objects is hidden inside the XS implementation, cloning/serialization is problematic. Tie::Hash::Indexed implements hooks for Storable, so cloning or serializing objects using Storable is safe.

Tie::Hash::Indexed tries very hard to detect any corruption in its data at runtime. So if something goes wrong, you'll most probably receive an appropriate error message.

If you find any bugs, Tie::Hash::Indexed doesn't seem to build on your system or any of its tests fail, please report the issue at <https://github.com/mhx/Tie-Hash-Indexed/issues>.

Copyright (c) Marcus Holland-Moritz. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See perltie, Hash::Ordered, Tie::IxHash.
2020-11-20 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.