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

In reply to Re: Link a hash and an array by bart
in thread Link a hash and an array by SkipHuffman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.