Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > trying to understand a hash

Reply
Thread Tools

trying to understand a hash

 
 
John
Guest
Posts: n/a
 
      10-07-2003
I'm taking apart somebody else's perl script in order to (a) learn and
(b) make something for my own purposes, and have come across a hash
that is not written in the manner to which I've become accustomed ...
the author is not within hollering distance, so I thought I'd try this
list for assistance.

Here's the hash as the author has created it:

$hashname{$key}{$other} = value;

where "$hashname" was initialized with "my %hashname", "$key" is a
scalar derived from input, and "$other" is another scalar also derived
from input.

It looks to me that "other" is outside of the key and obviously isn't
the value either. The script I found it in works as the author
intended, so the structure is obviously legal, but I don't understand
it. I was under the impression that a hash in scalar context should
look like:

$hashname{$key} = value;
or
%hashname(key => value);
or
%hashname("key","value");
but not
%hashname{$key}{$other} = value;

I thought maybe it was intended to be some kind of index notation, but
the actual value of that var is a string and not a digit, and if it
was supposed to be a reference of some sort to make or indicate the
key is another array (hash of hashes?), then I would expect the var
"$other" to be inside the key's curly braces, not outside in their own
braces (i.e. "$hashname{$key{$other}} = value;" as opposed to
"$hashname{$key}{$other} = value.

Can somebody enlighten me on what that {$other} is all about? How
does it work? Why does it work?

best regards,

John
 
Reply With Quote
 
 
 
 
James E Keenan
Guest
Posts: n/a
 
      10-07-2003

"John" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I'm taking apart somebody else's perl script in order to (a) learn and
> (b) make something for my own purposes,


Worthy objectives.

[snip]

> Here's the hash as the author has created it:
>
> $hashname{$key}{$other} = value;
>
> where "$hashname" was initialized with "my %hashname", "$key" is a
> scalar derived from input, and "$other" is another scalar also derived
> from input.
>
> It looks to me that "other" is outside of the key and obviously isn't
> the value either. The script I found it in works as the author
> intended, so the structure is obviously legal, but I don't understand
> it. I was under the impression that a hash in scalar context should
> look like:
>
> $hashname{$key} = value;
> or
> %hashname(key => value);
> or
> %hashname("key","value");
> but not
> %hashname{$key}{$other} = value;
>

It's a hash of hashes, a multi-dimensional data structure. The value
associated with $hashname{$key} is a *reference* to another hash. In that
inner hash, a value is being assigned. Example:

use strict;
use warnings;
use Data:umper;

my (%hashname, $key, $other);

$key = 'alpha';
$other = 'beta';

$hashname{$key}{$other} = 'gamma';

print Dumper(\%hashname);

See: perldoc perlref

jimk


 
Reply With Quote
 
 
 
 
Amir Kadic
Guest
Posts: n/a
 
      10-07-2003
John wrote:

> $hashname{$key}{$other} = value;


....which is the same as

$hashname{$key}->{$other}= value;

....because 'you can omit the arrow if and only if
it occurs between braces (or brackets)'.
I don't know where I read this, but hope it's correct.
Maybe `man perlreftut`...

Amir

 
Reply With Quote
 
Darren Dunham
Guest
Posts: n/a
 
      10-07-2003
John <(E-Mail Removed)> wrote:
> I'm taking apart somebody else's perl script in order to (a) learn and
> (b) make something for my own purposes, and have come across a hash
> that is not written in the manner to which I've become accustomed ...
> the author is not within hollering distance, so I thought I'd try this
> list for assistance.


> Here's the hash as the author has created it:


> $hashname{$key}{$other} = value;


> where "$hashname" was initialized with "my %hashname", "$key" is a
> scalar derived from input, and "$other" is another scalar also derived
> from input.


> It looks to me that "other" is outside of the key and obviously isn't
> the value either. The script I found it in works as the author
> intended, so the structure is obviously legal, but I don't understand
> it. I was under the impression that a hash in scalar context should
> look like:


> $hashname{$key} = value;
> or
> %hashname(key => value);
> or
> %hashname("key","value");
> but not
> %hashname{$key}{$other} = value;


This gets into references. You should go over the perlref and
perlreftut documents in perldoc if
you haven't already. The actual line is

$hashname{$key}{$other} = value;

and may also be written the following ways..

$hashname{$key}->{$other} = value;

$hashref = $hashname{$key}
${$hashref}{$other} = value;

%hashname has values in it that are not simple scalars, but are
references to more hashes.

%hashname = { key => { other => value } };


> I thought maybe it was intended to be some kind of index notation, but
> the actual value of that var is a string and not a digit, and if it
> was supposed to be a reference of some sort to make or indicate the
> key is another array (hash of hashes?), then I would expect the var
> "$other" to be inside the key's curly braces, not outside in their own
> braces (i.e. "$hashname{$key{$other}} = value;" as opposed to
> "$hashname{$key}{$other} = value.


Indeed "hash of hashes" is exactly how such a structure is named.

It's the way they're parsed. The above would require the presence of a
hash called %key.

Think of it like a multidimensional array.

$array[4][3] is also valid, but it's a array of arrays.

> Can somebody enlighten me on what that {$other} is all about? How
> does it work? Why does it work?


Start here.
perldoc perldsc
perldoc perlreftut
perldoc perlref

Come back if you have more questions...


--
Darren Dunham http://www.velocityreviews.com/forums/(E-Mail Removed)
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
Michael Budash
Guest
Posts: n/a
 
      10-07-2003
In article <(E-Mail Removed) >,
(E-Mail Removed) (John) wrote:

> I'm taking apart somebody else's perl script in order to (a) learn and
> (b) make something for my own purposes, and have come across a hash
> that is not written in the manner to which I've become accustomed ...
> the author is not within hollering distance, so I thought I'd try this
> list for assistance.
>
> Here's the hash as the author has created it:
>
> $hashname{$key}{$other} = value;
>
> where "$hashname" was initialized with "my %hashname", "$key" is a
> scalar derived from input, and "$other" is another scalar also derived
> from input.
>
> It looks to me that "other" is outside of the key and obviously isn't
> the value either. The script I found it in works as the author
> intended, so the structure is obviously legal, but I don't understand
> it. I was under the impression that a hash in scalar context should
> look like:
>
> $hashname{$key} = value;
> or
> %hashname(key => value);
> or
> %hashname("key","value");
> but not
> %hashname{$key}{$other} = value;
>
> I thought maybe it was intended to be some kind of index notation, but
> the actual value of that var is a string and not a digit, and if it
> was supposed to be a reference of some sort to make or indicate the
> key is another array (hash of hashes?), then I would expect the var
> "$other" to be inside the key's curly braces, not outside in their own
> braces (i.e. "$hashname{$key{$other}} = value;" as opposed to
> "$hashname{$key}{$other} = value.
>
> Can somebody enlighten me on what that {$other} is all about? How
> does it work? Why does it work?
>
> best regards,
>
> John


you were sooooo close. $hashname{$key}{$other} does indeed indicate a
"hash of hashes", or more specifically, a "hash of hash references",
since a hash value can only contain scalars.

so, since $hashname{$key} contains a hash ref, you can refer to one of
that hashref's keys in either of two ways:

$hashname{$key}->{$hashrefkey}

or

$hashname{$key}{$hashrefkey}

hth-
--
Michael Budash
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-08-2003
John <(E-Mail Removed)> wrote:

> the author is not within hollering distance, so I thought I'd try this
> list for assistance.



This is not a list (as in "email list"). This is a Usenet newsgroup.


> where "$hashname" was initialized with "my %hashname",



$hashname has no relationship to %hashname.

$hashname{} has a relationship to %hashname.

(but we know what you meant)


> I was under the impression that a hash in scalar context



A hash used in a scalar context is not useful to a Perl programmer,
it is only useful to a perl programmer.

(it returns 2 numbers with a slash between, try it:

print scalar(%hashname);
)


> should
> look like:
>
> $hashname{$key} = value;

^
^

That is not a hash. That is only one element of a hash.

("one thing" is what dollar sign means.)


"a hash" includes _all_ of the keys and values.

("all of the pairs" is what percent sign means)


> or
> %hashname(key => value);
> or
> %hashname("key","value");
> but not
> %hashname{$key}{$other} = value;



None of those are Perl (5).


> I thought maybe it was intended to be some kind of index notation,



Right!


> but
> the actual value of that var is a string and not a digit,



Perl has a data structure that is indexed by strings rather
than digits.

It is called "a hash".


You can tell it is a hash because it used curlies.

If it was an array at the 2nd level, it would have used squares.

$hashname{$key}[$other] = value; # $other better be a number


> and if it
> was supposed to be a reference of some sort to make or indicate the
> key is another array (hash of hashes?),

^^^^^^^^^^^^^^

Exactly.

I thought you said you didn't understand it?


> Can somebody enlighten me on what that {$other} is all about?



It is indexing a 2nd-level of hash.


> How
> does it work?



perldoc perlreftut


Put this in your code after %hashname has been loaded up, and you
can see its structure:

use Data:umper;
...
print Dumper \%hashname;


> Why does it work?



Because that's the way Larry wanted it?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      10-09-2003
(E-Mail Removed) (Tad McClellan) wrote in message news:<(E-Mail Removed)>.. .
>
> This is not a list (as in "email list"). This is a Usenet newsgroup.
>
>


My apologies, Tad, I didn't mean to denigrate this fine old pillar of
the internet .

> > or
> > %hashname(key => value);
> > or
> > %hashname("key","value");
> > but not
> > %hashname{$key}{$other} = value;

>
>
> None of those are Perl (5).
>


What do you mean? The first two structures above are shown in my perl
book which specifically refers to perl 5.005 (the book is from a
programming class I took several years ago ... I know it's old, but
it's still handy). I seem to recall being told that the "=>" (arrow)
sign within a hash structure was a perl v5 feature not found in
previous versions.

O&BTW, what did you mean when you said "perl programmer" versus "Perl
programmer"? Is there some kind of class warfare involving leading
upper-case characters that I wasn't aware of? My pound-bang
specifically says /usr/bin/perl and not /usr/bin/Perl ... ...
anyhow, I'm not a progammer -- I'm a pseudo-sysadmin that hacks out
perl scripts to aid me with my daily grind, but I have not got (nor do
I desire) the depth of knowledge in any programming language to be
termed "a programmer".

No worries ... all the responses to my query were very helpful ... now
I comprehend the hash of hashes structure a bit better than before,
and that is a good thing.

So ... to summarize, the structure

%hashname{$key}{$other} = value;

represents a 'hash of hashes' where the hash defined as
"%hashname{$key}" dereferences to another hashname which is then
referenced by key "$other" to get (or set) the value "value".

It makes sense now that I look at it in that light, but my initial
confusion was probably caused a bit by not understanding the the order
of operation ... to use the HoH as above, the hash has to dereference
from left to right (first complete legal term dereferences before any
further terms are analyzed?) whereas I'm used to reading these things
from right to left ... I was thinking that the "$other" was a
modifier, or reference, to the term "$key", not the hash
"%hashname{$key}", and that was an incorrect belief on my part. Holy
cr@p, batman! Now all of a sudden all kinds of funky hashes I've seen
are starting to make sense!!!!! Whoa ... my head is spinning ...


Best regards,

John
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-09-2003
John <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Tad McClellan) wrote in message news:<(E-Mail Removed)>.. .
>>
>> This is not a list (as in "email list"). This is a Usenet newsgroup.
>>

>
> My apologies, Tad, I didn't mean to denigrate this fine old pillar of
> the internet .



I point out the distinction not because mailing lists are "bad"
or "worse".

I point out the distinction because they are different dynamics,
with different rules of what is socially acceptable.


>> > or
>> > %hashname(key => value);
>> > or
>> > %hashname("key","value");
>> > but not
>> > %hashname{$key}{$other} = value;

>>
>>
>> None of those are Perl (5).
>>

>
> What do you mean?



$ perl -e '%hashname(key => value)'
syntax error at -e line 1, near "%hashname("
Execution of -e aborted due to compilation errors.

$ perl -e '%hashname("key","value")'
syntax error at -e line 1, near "%hashname("
Execution of -e aborted due to compilation errors.

$ perl -e '%hashname{$key}{$other} = value'
syntax error at -e line 1, near "%hashname{"
Execution of -e aborted due to compilation errors.


I mean that none of them are written in the Perl programming language.

I also mean that they _will_ be part of the Perl programming
language in a few more years, when Perl 6 is released.


> The first two structures above are shown in my perl
> book



It is a crap book then. Syntax errors are inexcusable.


> which specifically refers to perl 5.005 (the book is from a
> programming class I took several years ago ... I know it's old, but
> it's still handy).



If it teaches non-Perl as Perl, then it is moving you _backwards_
in your understanding. Throw it in the trash (or sell it on E-bay).


> I seem to recall being told that the "=>" (arrow)
> sign within a hash structure was a perl v5 feature not found in

^^^^^^^^^^^^^^^^^^^^^^^
> previous versions.



The "fat comma" ( => ) has nothing to do with a hash structure,
it just happens that that is where most people choose to use it.

(and yes, it was introduced in version 5.mumble-mumble).

It is just another way of writing a comma (with a little extra
treatment for the left-hand operand).

print 'hi' , ' ' , 'there';

print 'hi' => ' ' => 'there';

print hi => ' ' => there;

all make the same output.


> O&BTW, what did you mean when you said "perl programmer" versus "Perl
> programmer"?



That Question is Asked Frequently:

What's the difference between "perl" and "Perl"?


A hash in a scalar context is only useful to the C programmers
that work on the perl binary. It is useful for evaluating the
effectiveness of perl's built-in hashing algorithm.


> Is there some kind of class warfare involving leading
> upper-case characters that I wasn't aware of?



No, there is some kind of different semantic between when
you say "perl" and when you say "Perl".


> My pound-bang
> specifically says /usr/bin/perl and not /usr/bin/Perl ... ...



Which illustrates the difference rather nicely, as it is referring
to the perl binary (the interpreter) rather than to the Perl
programming language.


> %hashname{$key}{$other} = value;



That still is not Perl. You must have meant this instead:

$hashname{$key}{$other} = 'value'; # barewords are "bad" too
^
^

> represents a 'hash of hashes' where the hash defined as
> "%hashname{$key}" dereferences to another hashname which is then

^^^^

No, it refers to another hash, whether that hash has a name or not.

The name of something is not the same as the something being named.

You can have named or anonymous hashes in Perl.


> Holy
> cr@p, batman! Now all of a sudden all kinds of funky hashes I've seen
> are starting to make sense!!!!! Whoa ... my head is spinning ...



Welcome to our (Perl) world!


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Michael P. Broida
Guest
Posts: n/a
 
      10-09-2003
John wrote:
> So ... to summarize, the structure
>
> %hashname{$key}{$other} = value;
>
> represents a 'hash of hashes' where the hash defined as
> "%hashname{$key}" dereferences to another hashname which is then
> referenced by key "$other" to get (or set) the value "value".


Hmm. I missed the original post, but I don't think that:
%hashname{$key}
is valid anywhere. I've been doing a LOT of hash-of-
hash-of-hash-of-hash-of-hash-etc stuff these past few
months, and in NO case did it work with a leading
percent-sign. Perhaps there's some case where the
above syntax works, but I haven't seen it yet.

However,
$hashname{$key}
is valid. And:
$hashname{$key}{$other} = value;
can work as "hash of hashes". In both cases, the
declaration of the hash is:
%hashname

My point is that the "%" is used to declare the hash
or when referring to the -entire- hash. To look at any
part of the hash, you have to use the "$" and a key
in curlies.

Mike
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-09-2003
Tad McClellan wrote:
> A hash used in a scalar context is not useful to a Perl
> programmer, it is only useful to a perl programmer.
>
> (it returns 2 numbers with a slash between, try it:
>
> print scalar(%hashname);
> )


To me it may be useful to check whether the hash is empty:

if (%hash) {
do this;
} else {
do that;
}

And I can assure you that I'm not a perl programmer.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Trying to Understand Layer 2 scadav Cisco 6 07-07-2005 05:24 PM
trying to understand hash of hashes or multidimensional arrays Sherm Pendley Perl Misc 8 06-23-2005 11:40 PM



Advertisments