Question about this data structure...
Question about this data structure...
am 25.09.2007 02:33:34 von James Egan
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'],
],
};
Re: Question about this data structure...
am 25.09.2007 07:06:02 von Iain Chalmers
In article ,
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?
>
> -Thanks
>
>
> $rec =
> {
> fields => {
> 'FIRST_NAME' => 0,
> 'LAST_NAME' => 1,
> 'ADDRESS' => 2,
> },
>
> encoding => [
> [6, 18, 'C'],
> [26, 1, 'C'],
> [46, 8, 'D'],
> ],
> };
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)
Re: Question about this data structure...
am 25.09.2007 08:09:53 von Mirco Wahab
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.
Re: Question about this data structure...
am 25.09.2007 14:45:28 von Paul Lalli
On Sep 24, 8:33 pm, 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'],
> ],
> };
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