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

Ok, then next question. My situation is that I have a set of data that I would like to access both in an array, and in a hash. Basically the fields have both an ordinal reference, and a named reference. Is there a way to link a data set to both a hash and an array.

Lets say this is my array.

my @array=(Bob, Smith, 1234 Main St, Anytown, 20500);

Or this is my hash.

my %hash=( firstname => Bob, lastname => Smith, address => 1234 Main St, town => AnyTown, zip => 20500);

I can access the information from either one.

print "His name is $array[0] $array[1]\n;

or

print "His name is $hash[firstname] $hash[lastname]\n;

But if I change one the other will, of course, not change.

$array(0)="Robert" print "His name is $hash[firstname] $hash[lastname]\n; #wrong result

Is there a way to tie these two data structures together?

Thank you,

Skip

Replies are listed 'Best First'.
Re: Link a hash and an array
by kappa (Chaplain) on Mar 11, 2004 at 20:02 UTC
    Remember, arrays are usually faster to update than hashes, but you totally lose all benefits if updating array also involves syncing to a hash (via hooking update code using tie). I would rather add a level of indirection by storing references to the data in the hash.
    @array = qw/Kitty Kate/; %hash = ( firstname => \$array[0], lastname => \$array[1], ); $$hash{firstname} = 'Murka'; is($array[0], 'Murka');

      That is close, but not exactly what I want to do.

      I did not put enough detail in my starting post.

      I have an array of values, the first one of which indicates what sort of data is in the array. I also have a set of arrays that indicate what fieldnames should be associate with the array.

      Think of the array as sometimes being name and address, and sometimes name and phone number. I want to read $ARRAY[0] and then decide how the hash will be created. What I tried to do that does not work is this:

      @Address=qw(type firstname lastname streetaddress city zip); @Phone=qw(type name phonenumber); %Stuff; sub ReadStuff{ if ($@_[0]='addr') {%Stuff(@Address)=@_}; elsif ($@_[0]='phone'){%Stuff(@Phone)=@_}; ...do something useful... } @data=(addr, bob, smith, 1234 main st, anytown, 20500); &ReadStuff(@data); @data=(phone, bob, smith, 212-555-1212) &ReadStuff(@data)
      But, of course, that does not work exactly like that, so I am trying to get close. Skip
        But, of course, that does not work exactly like that

        It would probably help if you used syntactically valid Perl code. If you did, you might be surprised to find that what you have works fine. Here I've gone through the trouble to port it for you:

        #!/usr/bin/perl use strict; # safety checks use warnings; # useful hints use Data::Dumper; # notice the use of 'my' to declare a lexical scope my @Address=qw(type firstname lastname streetaddress city zip); my @Phone=qw(type name phonenumber); sub ReadStuff { # instead of using funny global variables, scope the # hash within the subroutine, and return a reference at # the end my %Stuff; # $_[0] is the first element of the array @_ # eq is the string equality test # @h{@k} = @v is the syntax for hash slices if ($_[0] eq 'addr') { @Stuff{@Address} = @_ } elsif ($_[0] eq 'phone') { @Stuff{@Phone} = @_ } # return a reference (as mentioned before) return \%Stuff; } # let's quote our list elements this time my @data1 = ('addr', 'bob', 'smith', '1234 main st', 'anytown', '20500 +'); my $stuff1 = ReadStuff(@data1); my @data2 = ('phone', 'bob', 'smith', '212-555-1212'); my $stuff2 = ReadStuff(@data2); # dump the results from both runs print Dumper $stuff1; print Dumper $stuff2;

        Update: while this code does work, it doesn't quite do what your original question asks. That said, what you have here is probably exactly what I would suggest -- that is, keep the data in one place, and a list of keys in the other to tie it together. Using the structures this code creates, you could, for example, do $Stuff1->{$Address[4]} to get the field from $Stuff1 that corresponds to the key at index 4 from @Address. (whew! try saying that 5 times fast!)

        Maybe like this (without any error checking):
        our @Address=qw(type firstname lastname streetaddress city zip); our @Phone=qw(type name phonenumber); our %Types = ( "addr" => \@Address, "phone" => \@Phone ); our %Stuff; sub ReadStuff{ @Stuff{@{$Types{$_[0]}}} = @_; #...do something useful... }

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: Link a hash and an array
by Vautrin (Hermit) on Mar 11, 2004 at 19:50 UTC
    Check out Tie::IxHash for a Perl module that looks like it might address your problems.

    Want to support the EFF and FSF by buying cool stuff? Click here.
Re: Link a hash and an array
by bart (Canon) on Mar 11, 2004 at 22:34 UTC
    print "His name is $hash[firstname] $hash[lastname]\n;
    I'm sorry, you probably mean
    print "His name is $hash{firstname} $hash{lastname}\n;
    This isn't Javascript (or PHP), you know...

    But anyway, your wish sounds pretty much up the alley of the pseudo hashes, which act like hashes but actually are arrays — and which are considered obsolete. The new interface is use fields, and even though my code still acts the same for perls 5.6.1 and 5.8.0, I do have doubts about its viability for the future.

    Currently the hash with the array indices is in the array element with index 0; normal elements start at index 1. So I think it's not unlikely that the index might shift, if this element were to drop away. I really think you shouldn't actually be accessing the elements this way...

    Anyway, take this at its face value: a nice experiment, but preferably don't use it in actual production code. BTW I'd really appreciate it if someone more knowledgable on the internals, and future of pseudo-hashes could shed some light on this.

    p.s. I'm not actually sure I even call new in the proper way...

    #!/usr/local/bin/perl -w { package Record; use fields qw(firstname lastname address town zip); sub new { my $this = shift->fields::new; %$this = @_; return $this; } } my Record $data = new Record( firstname => 'Bob', lastname => 'Smith', address => '1234 Main St', town => 'AnyTown', zip => 20500); use Data::Dumper; print Dumper $data; print "His name is $data->{firstname} $data->{lastname}\n"; print "His name is $data->[1] $data->[2]\n";
Re: Link a hash and an array
by Fletch (Bishop) on Mar 11, 2004 at 19:39 UTC

    perldoc perltie