[Neworleans-pm] Tie::Hash::Indexed question - SOLVED

B. Estrade estrabd at mailcan.com
Tue Nov 4 09:29:42 PST 2008


And the winning solution:

#!/bin/env perl

use strict;
use Tie::Hash::Indexed;

sub tieH {
  tie my %hash, 'Tie::Hash::Indexed', @_;
  return \%hash;
}

tie my %test, 'Tie::Hash::Indexed';

%test = ( 1 => tieH( a => 100,
                     b => 200,
                     c => 300),
          2 => tieH( d => 400,
                     e => 500,
                     f => 600),
          3 => tieH( g => 700,
                     h => 800,
                     i => 900),
         );

foreach my $key (keys(%test)) {
  print "$key\n";
  foreach my $ikey (keys(%{$test{$key}})) {
    print "  $ikey\n";
  }
}

1;

On Tue, Nov 04, 2008 at 11:20:11AM -0600, B. Estrade wrote:
> Okay, now I feel silly - I think I found the answer at PerlMonks :)...
> 
> http://www.perlmonks.org/?displaytype=print;node_id=485306;replies=1
> 
> Cheers,
> Brett
> 
> On Tue, Nov 04, 2008 at 11:17:14AM -0600, B. Estrade wrote:
> > I can't seem to find it via Dr. Google, and before I go to IRC or PM, I wanted to ask here.
> > 
> > If I use Tie::Hash::Indexed to define an ordered hash, should this apply to all hashes in a nested data structure? The following example does not propagate this to nested hashes:
> > 
> > #!/bin/env perl
> > use strict;
> > use Tie::Hash::Index;
> > 
> > tie my %test, 'Tie::Hash::Indexed';
> > %test = ( 1 => { a => 100,
> >                  b => 200,
> >                  c => 300, },
> >           2 => { d => 400,
> >                  e => 500,
> >                  f => 600, }, 
> >           3 => { g => 700,
> >                  h => 800,
> >                  i => 900, },
> >          ); 
> > 
> > foreach my $key (keys(%test)) {
> >   print "$key\n";
> >   foreach my $ikey (keys(%{$test{$key}})) {
> >     print "  $ikey\n";
> >   }
> > }
> > 
> > 1;
> > __END__
> > 
> > The output looks like:
> > 
> > 1
> >   c
> >   a
> >   b
> > 2
> >   e
> >   d
> >   f
> > 3
> >   h
> >   g
> >   i
> > 
> > But I want:
> > 
> > 1
> >   a
> >   b
> >   c
> > 2
> >   d
> >   e
> >   f
> > 3
> >   h
> >   i 
> >   j
> > 
> > And this indicates that the nested hashs are not 'tie'd. Does anyone one know if there is something I can do when 'tie'ing the root hash so that this property is passed down? If not, how would I 'tie' and anonymous hash?
> > 
> > And before anyone tells me that I should be using an array, note that I am writing up a test for something that doesn't required ordered keys to be expressed. I am trying to work within existing code to generate a "worst-case" hash order, and for that I need to make sure that certain keys are accessed in-order.
> > 
> > Cheers,
> > Brett
> > 
> > 
> > 
> > 
> > -- 
> > B. Estrade
> > Louisiana Optical Network Initiative
> > +1.225.578.1920 aim: bz743
> > :wq
> > _______________________________________________
> > NewOrleans-pm mailing list
> > NewOrleans-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/neworleans-pm
> 
> -- 
> B. Estrade
> Louisiana Optical Network Initiative
> +1.225.578.1920 aim: bz743
> :wq
> _______________________________________________
> NewOrleans-pm mailing list
> NewOrleans-pm at pm.org
> http://mail.pm.org/mailman/listinfo/neworleans-pm

-- 
B. Estrade
Louisiana Optical Network Initiative
+1.225.578.1920 aim: bz743
:wq


More information about the NewOrleans-pm mailing list