Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about this data structure...

4 views
Skip to first unread message

James Egan

unread,
Sep 24, 2007, 8:33:34 PM9/24/07
to
I found the data structure below somewhere on the Internet. What type of
data structure is it, and how do I access elements of it? For example,
how would I print FIRST_NAME?

-Thanks


$rec =
{
fields => {
'FIRST_NAME' => 0,
'LAST_NAME' => 1,
'ADDRESS' => 2,
},

encoding => [
[6, 18, 'C'],
[26, 1, 'C'],
[46, 8, 'D'],
],
};

Iain Chalmers

unread,
Sep 25, 2007, 1:06:02 AM9/25/07
to
In article <TPqdnd-aNczDymXb...@comcast.com>,
James Egan <jega...@comcast.net> wrote:

print $rec->{fields}{FIRST_NAME}; #should print 0

also

print $rec->{encoding}[1][0]; #should print 26

cheers,

big

--
"Everything you love, everything meaningful with depth and history,
all passionate authentic experiences will be appropriated, mishandled,
watered down, cheapened, repackaged, marketed and sold to the people
you hate." Mr Jalopy quoting Hooptyrides (on jalopyjunktown.com)

Mirco Wahab

unread,
Sep 25, 2007, 2:09:53 AM9/25/07
to
James Egan wrote:
> I found the data structure below somewhere on the Internet. What type of
> data structure is it, and how do I access elements of it? For example,
> how would I print FIRST_NAME?
> $rec =
> {
> fields => {
> 'FIRST_NAME' => 0,
> 'LAST_NAME' => 1,
> 'ADDRESS' => 2,
> },
>
> encoding => [
> [6, 18, 'C'],
> [26, 1, 'C'],
> [46, 8, 'D'],
> ],
> };

$rec is a hash reference, the hash it addresses
"contains" an array and another hash, so $rec
is a reference to "hash of hash and array " (HoHaA)

fields is, as said, a hash reference and points to a hash,
encoding is an array reference in itself and points to
another "array of arrays" (AoA).

Access to fields:
print $rec->{fields}->{LAST_NAME};

which can be written as
print $rec->{fields}{LAST_NAME};

Access to encoding:
print $rec->{encoding}->[0]->[2]; # array[0][2], 'D'

which can be written as:
print $rec->{encoding}[0][2];

Regards

M.

Paul Lalli

unread,
Sep 25, 2007, 8:45:28 AM9/25/07
to
On Sep 24, 8:33 pm, James Egan <jegan...@comcast.net> wrote:
> I found the data structure below somewhere on the Internet.
> What type of data structure is it, and how do I access elements
> of it? For example, how would I print FIRST_NAME?

> $rec =


> {
> fields => {
> 'FIRST_NAME' => 0,
> 'LAST_NAME' => 1,
> 'ADDRESS' => 2,
> },
>
> encoding => [
> [6, 18, 'C'],
> [26, 1, 'C'],
> [46, 8, 'D'],
> ],
> };

Buckle in. . .

$rec is a reference to a hash.
The hash that $rec references, known as %{$rec}, contains two key/
value pairs.
The first key listed in %{$rec} above is 'fields'. The value in %
{$rec} associated with the key 'fields', known as $rec->{fields}, is a
reference to a hash.
The hash that $rec->{fields} references, known as %{$rec->{fields}},
contains three key/value pairs.
The first key listed in %{$rec->{fields}} is 'FIRST_NAME'. The value
associated with this key, known as $rec->{fields}{FIRST_NAME}, is 0.

The second key listed in %{$rec} above is 'encoding'. The value in %
{$rec} associated with the key 'encoding', known as $rec->{encoding},
is a reference to an array.
The array that $rec->{encoding} references, known as @{$rec-
>{encoding}}, contains three values.
Each of the three values of @{$rec->{encoding}} are references to
arrays. The first of these values is $rec->{encoding}[0].
The array that $rec->{encoding}[0] references, known as @{$rec-
>{encoding}[0]}, contains three values: 6, 18, and 'C'.
You can access the second value, 18, of this array as: $rec->{encoding}
[0][1].

For further details and explanations, see the tutorials and references
in perldoc:
perldoc perlreftut
perldoc perllol
perldoc perldsc

Paul Lalli

0 new messages