perldoc > Tie::IxHash

📖 NAME

Tie::IxHash — ordered associative arrays for Perl

🚀 Quick Reference

Use CaseCommandDescription
Create a tied ordered hashtie %hash, 'Tie::IxHash', LISTBind a hash to preserve insertion order
Create an IxHash object$obj = Tie::IxHash->new(LIST)OO interface for more control
Add element to end$obj->Push(key => value)Append a key-value pair
Remove last element($k, $v) = $obj->PopPop like a Perl array
Add element to beginning$obj->Unshift(key => value)Prepend a key-value pair
Remove first element($k, $v) = $obj->ShiftShift like a Perl array
Insert or replace elements$obj->Splice(OFFSET, LENGTH, LIST)Array splice on key-value pairs
Get all keys or values@keys = $obj->Keys; @vals = $obj->ValuesFetch keys or values in order
Get indices for keys@idx = $obj->Indices(@keys)Find positions of given keys
Delete elements by key$obj->Delete(@keys)Remove specified keys
Replace element at index$obj->Replace(OFFSET, VALUE, KEY?)Substitute value and optionally key
Reorder elements$obj->Reorder(@keys)Set a custom order (keys not in list are removed)
Sort by key$obj->SortByKeyAlphabetical order by key
Sort by value$obj->SortByValueAlphabetical order by value
Get number of elements$len = $obj->LengthCount of key-value pairs
Clear all elements$obj->ClearReset to empty

📋 SYNOPSIS

# simple usage
use Tie::IxHash;
tie HASHVARIABLE, 'Tie::IxHash' [, LIST];

# OO interface with more powerful features
use Tie::IxHash;
TIEOBJECT = Tie::IxHash->new( [LIST] );
TIEOBJECT->Splice( OFFSET [, LENGTH [, LIST]] );
TIEOBJECT->Push( LIST );
TIEOBJECT->Pop;
TIEOBJECT->Shift;
TIEOBJECT->Unshift( LIST );
TIEOBJECT->Keys( [LIST] );
TIEOBJECT->Values( [LIST] );
TIEOBJECT->Indices( LIST );
TIEOBJECT->Delete( [LIST] );
TIEOBJECT->Replace( OFFSET, VALUE, [KEY] );
TIEOBJECT->Reorder( LIST );
TIEOBJECT->SortByKey;
TIEOBJECT->SortByValue;
TIEOBJECT->Length;

📝 DESCRIPTION

This Perl module implements Perl hashes that preserve the order in which the hash elements were added. The order is not affected when values corresponding to existing keys in the IxHash are changed. The elements can also be set to any arbitrary supplied order. The familiar perl array operations can also be performed on the IxHash.

🔧 Standard "TIEHASH" Interface

The standard "TIEHASH" mechanism is available. This interface is recommended for simple uses, since the usage is exactly the same as regular Perl hashes after the tie is declared.

🧩 Object Interface

This module also provides an extended object-oriented interface that can be used for more powerful operations with the IxHash. The following methods are available:

💡 EXAMPLE

use Tie::IxHash;

# simple interface
$t = tie(%myhash, 'Tie::IxHash', 'a' => 1, 'b' => 2);
%myhash = (first => 1, second => 2, third => 3);
$myhash{fourth} = 4;
@keys = keys %myhash;
@values = values %myhash;
print("y") if exists $myhash{third};

# OO interface
$t = Tie::IxHash->new(first => 1, second => 2, third => 3);
$t->Push(fourth => 4); # same as $myhash{'fourth'} = 4;
($k, $v) = $t->Pop;    # $k is 'fourth', $v is 4
$t->Unshift(neg => -1, zeroth => 0);
($k, $v) = $t->Shift;  # $k is 'neg', $v is -1
@ 2, foo => 100, bar => 101);

@keys = $t->Keys;
@values = $t->Values;
@indices = $t->Indices('foo', 'zeroth');
@itemkeys = $t->Keys(@indices);
@itemvals = $t->Values(@indices);
$t->Replace(2, 0.3, 'other');
$t->Delete('second', 'zeroth');
$len = $t->Length;     # number of key-value pairs

$t->Reorder(reverse @keys);
$t->SortByKey;
$t->SortByValue;

🐛 BUGS

You cannot specify a negative length to Splice. Negative indexes are OK, though.

📌 NOTE

Indexing always begins at 0 (despite the current $[ setting) for all the functions.

📋 TODO

Addition of elements with keys that already exist to the end of the IxHash must be controlled by a switch.

Provide TIEARRAY interface when it stabilizes in Perl.

Rewrite using XSUBs for efficiency.

👤 AUTHOR

Gurusamy Sarathy gsar AT umich.edu

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

🏷️ VERSION

Version 1.23

🔗 SEE ALSO

perl(1)

Tie::IxHash
📖 NAME 🚀 Quick Reference 📋 SYNOPSIS 📝 DESCRIPTION
🔧 Standard "TIEHASH" Interface 🧩 Object Interface
💡 EXAMPLE 🐛 BUGS 📌 NOTE 📋 TODO 👤 AUTHOR 🏷️ VERSION 🔗 SEE ALSO

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-25 17:55 @216.73.217.127
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^