in reply to Link a hash and an array

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";
Result (both for 5.6.1 and 5.8.0):
$VAR1 = bless( [ { 'firstname' => 1, 'zip' => 5, 'town' => 4, 'address' => 3, 'lastname' => 2 }, 'Bob', 'Smith', '1234 Main St', 'AnyTown', 20500 ], 'Record' ); His name is Bob Smith His name is Bob Smith