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

I think I must be missing something obvious because retrieving a value from a Tie::IxHash seems awfully convoluted:

my $hash = Tie::IxHash->new; $hash->Push('one' => 1, 'two' => 2); my @indices = $hash->Indices('two'); my @results = $hash->Values(@indices);

It seems like I should be able to do my $value = $hash->Values('two'); but the documentation doesn't mention that syntax.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Retrieving a single value from a Tie::IxHash object (updated)
by haukex (Archbishop) on Oct 09, 2017 at 19:37 UTC

    Granted, the Tie::IxHash documentation isn't quite clear on this, but the point of Perl's tie interface is to provide variables that look like regular variables from the outside, but can act differently. Tie::IxHash mostly behaves like a regular hash, except that it remembers the order of keys. So perhaps it's easiest to just do the following?

    use Tie::IxHash; tie my %hash, 'Tie::IxHash', one => 1, two => 2; print "$hash{two}\n"; # prints "2"

    Update: Another thing the documentation doesn't seem to mention is that you can use both interfaces, via tied:

    use Data::Dump; dd tied(%hash)->Indices(qw/two one/); # prints (1, 0)
Re: Retrieving a single value from a Tie::IxHash object
by NetWallah (Canon) on Oct 09, 2017 at 19:17 UTC
    FWIW, the documentation DOES say:
    If a single argument is given, returns the single value corresponding to the index. This is usable in either scalar or list context.

                    All power corrupts, but we need electricity.

      Shoulda RTFM more closely. $hash->FETCH is the method I needed.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

      I should have tested this more thoroughly. No matter what I put in the for the argument to Values, it always returns the first element.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Retrieving a single value from a Tie::IxHash object
by nysus (Parson) on Oct 09, 2017 at 19:06 UTC

    Retracted. You can do the syntax I mentioned. Vote me down, I can take it.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Retraction of retraction. See my comment below. $hash->FETCH does the trick.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

      Voted you up (or rather voted up your posts), because, thanks to you, I discovered a module which I did not know.