Array -> Reference to Hash of Arrays

T

Tom

Hope my subject is right !
Well, Here is what 'm trying to fight with

How to create/add/delete/print an "Array which Refers to Hash of
Arrays"

Array - List of test cases (Not-Unique)
Hash - Key: Test Case Iteration # (Unique)
Value: <More than one value> i.e Array

E.g. One test case(ARRAY-ELEMENT) shall have more than one iterations
(HASH-KEY) and iteration results (HASH-VALUE). I'm fiddling with perl
datastructures on how to implement this.

Let me know if this needs more explanation !
 
B

Brad Baxter

Hope my subject is right !
Well, Here is what 'm trying to fight with

How to create/add/delete/print an "Array which Refers to Hash of
Arrays"

Array - List of test cases (Not-Unique)
Hash - Key: Test Case Iteration # (Unique)
Value: <More than one value> i.e Array

E.g. One test case(ARRAY-ELEMENT) shall have more than one iterations
(HASH-KEY) and iteration results (HASH-VALUE). I'm fiddling with perl
datastructures on how to implement this.

Let me know if this needs more explanation !

And you've read "perldoc perldsc" sections "HASHES OF ARRAYS" and "ARRAYS
OF HASHES" and you're not sure how to put them together? What have you
tried so far?

Regards,

Brad
 
T

Tad McClellan

Tom said:
How to create/add/delete/print an "Array which Refers to Hash of
Arrays"


That would commonly be called a LoHoL (list of hashes of lists),
but more precisely called "array of references to hashes of array
references" I guess.

Have you seen the references tutorial yet?

perldoc perlreftut

Or the other docs about complex data structures?

perldoc perllol
perldoc perldsc


Let me know if this needs more explanation !


Let us know if perlreftut doesn't do it for you.
 
B

Brian McCauley

Tom said:
Hope my subject is right !
Well, Here is what 'm trying to fight with

How to create/add/delete/print an "Array which Refers to Hash of
Arrays"

Array - List of test cases (Not-Unique)
Hash - Key: Test Case Iteration # (Unique)
Value: <More than one value> i.e Array

If "Test Case Iteration #" is simply a sequential integer then it would
be more natural to use an array rather than a hash for your second level
structure.

If the "<More than one value>" are disimilar things like "duration",
"memory usage", "number of rabits pulled from hat" then it is unnatural
to put them in an array and would be more natural to use a hash.

Only if "<More than one value>" is a list of like values does an array
seem the natural choice. e.g. "weight of first rabit","weight of second
rabit" ... "weight of last rabit".

Anyhow, as others have said, if you find something in the documentation
unclear then please tell us precicely what so that we can have a chance
to improve the documentation.
 
E

Eric Schwartz

wana said:
@my_array;
%my_hash;
@my_main_array;

push @my_main_array, \%my_hash;
%{$my_main_array[0]}{\@my_array}++;

the hash value associated with key \@my_array would be 1 now, right?

That would be the hash value associated with the stringified
representation of \@my_array. Hash keys are strings. From 'perldoc
perldata':

Hashes are unordered collections of scalar values indexed by their
associated string key.

So, anything that is not a string, that is used as a hash key, is
turned into one.
And if
I do:

push @my_main_array, \%my_hash;
%{$my_main_array[1]}{\@my_array}++;

I have a new hash as my second element of @my_main_array whose key is a
reference to a new array.

No. What you have is another reference to the same hash as in
$my_main_array[0]. Think about it this way: you took a reference to a
hash once, and got, well, a reference that points to that hash. Why,
when you take a reference to that same hash again, would you expect
that reference to point to a new hash?

Likewise, with \@my_array, you're stringifying the representation of
the arrayref, and using that as the key. Since $my_main_array[0]
points to the same hash as $my_main_array[1]; you've just incremented
the value indexed by that key in %my_hash.
In C++, this would not work the same because all
references to %my_hash and @my_array would just point to the same hash and
array. Is this right?

That's exactly how it works in Perl, as well. I'm not quite sure how
a reading of 'perldoc perlreftut' or 'perldoc perlref' could give you
the impression that it worked the way you seem to think it works, but
if you can explain which bits are confusing, we might be able to
suggest better wording so that everyone can benefit from your
confusion.

-=Eric
 
B

Brad Baxter

Brad Baxter wrote:
[snip]
On Fri, 5 Nov 2004, Tom wrote:
[snip]


@my_array;
%my_hash;
@my_main_array;

push @my_main_array, \%my_hash;
%{$my_main_array[0]}{\@my_array}++;

the hash value associated with key \@my_array would be 1 now, right? And if
I do:

push @my_main_array, \%my_hash;
%{$my_main_array[1]}{\@my_array}++;

I have a new hash as my second element of @my_main_array whose key is a
reference to a new array. In C++, this would not work the same because all
references to %my_hash and @my_array would just point to the same hash and
array. Is this right? the concept seems counter-intuitive and can be a
little confusing.

wana

Are you the original poster? (i.e., Tom?)

Brad
 
W

wana

Brad said:
And you've read "perldoc perldsc" sections "HASHES OF ARRAYS" and "ARRAYS
OF HASHES" and you're not sure how to put them together? What have you
tried so far?

Regards,

Brad

@my_array;
%my_hash;
@my_main_array;

push @my_main_array, \%my_hash;
%{$my_main_array[0]}{\@my_array}++;

the hash value associated with key \@my_array would be 1 now, right? And if
I do:

push @my_main_array, \%my_hash;
%{$my_main_array[1]}{\@my_array}++;

I have a new hash as my second element of @my_main_array whose key is a
reference to a new array. In C++, this would not work the same because all
references to %my_hash and @my_array would just point to the same hash and
array. Is this right? the concept seems counter-intuitive and can be a
little confusing.

wana
 
E

Eric Schwartz

wana said:
@my_array;
%my_hash;
@my_main_array;

push @my_main_array, {%my_hash};
%{$my_main_array[0]}{[@my_array]}++;

Argh, to quote (possibly apocryphally) Niels Bohr: "That isn't right.
It's not even wrong!"

Read my reply again, closer. $hash{[@array]} does NOT use the
reference to the anonymous array as a key! Hashes ONLY use strings as
keys! What you're doing first stringifies the reference, and then
uses THAT as the key to the hash. What this means is threefold:

1) You can't turn a stringified reference back into a reference
(possibly you could, with XS, I'm not sure, but see below).
2) Even if you could, since stringifying a reference doesn't increment
its refcount, it's possible-- likely, even-- that the thing thus
referred to wouldn't exist any longer.
3) You don't appear to be interested in reading what I have to say, or
you would have seen that I addressed that very topic as the VERY
FIRST thing I commented on!

I don't like it first of all when I spend time reading the docs
that they have on their very own hard drive to someone, but I like
it even less when I do so and they ignore me!

If you want to get help from people on this newsgroup, read the
docs FIRST, and ask questions about them if necessary, and READ
what people write when they correct you!
Is it correct to use the '++' at the end to create a value for the hash?

That's perfectly fine; why souldn't it be?
Does this give it a value of one?

It increments whatever value was there; if it was 0, (or undef, which
is 0 when treated as a number), it becomes one, yes; what do you think
happens?
I have seen this notation used commonly
with creating hash elements and I was not totally sure.

Did you think of reading the documentation, which already exists on
your hard drive, before asking other people to read it for you?
Everything you've wanted to know so far has been readily answered in
the perlref, perlreftut, and perldata manpages. I don't mind
explaining things you've read there, and didn't understand, but when
it seems that you haven't read them at all, it's very frustrating.

-=Eric
 
W

wana

Eric said:
wana said:
@my_array;
%my_hash;
@my_main_array;

push @my_main_array, \%my_hash;
%{$my_main_array[0]}{\@my_array}++;

the hash value associated with key \@my_array would be 1 now, right?

That would be the hash value associated with the stringified
representation of \@my_array. Hash keys are strings. From 'perldoc
perldata':

Hashes are unordered collections of scalar values indexed by their
associated string key.

So, anything that is not a string, that is used as a hash key, is
turned into one.
And if
I do:

push @my_main_array, \%my_hash;
%{$my_main_array[1]}{\@my_array}++;

I have a new hash as my second element of @my_main_array whose key is a
reference to a new array.

No. What you have is another reference to the same hash as in
$my_main_array[0]. Think about it this way: you took a reference to a
hash once, and got, well, a reference that points to that hash. Why,
when you take a reference to that same hash again, would you expect
that reference to point to a new hash?

Likewise, with \@my_array, you're stringifying the representation of
the arrayref, and using that as the key. Since $my_main_array[0]
points to the same hash as $my_main_array[1]; you've just incremented
the value indexed by that key in %my_hash.
In C++, this would not work the same because all
references to %my_hash and @my_array would just point to the same hash
and
array. Is this right?

That's exactly how it works in Perl, as well. I'm not quite sure how
a reading of 'perldoc perlreftut' or 'perldoc perlref' could give you
the impression that it worked the way you seem to think it works, but
if you can explain which bits are confusing, we might be able to
suggest better wording so that everyone can benefit from your
confusion.

-=Eric


I thought that I had read otherwise but I cannot find the source. A quick
re-read of perlreftut proves me wrong and reveals a possible solution to
save myself partially:

@my_array;
%my_hash;
@my_main_array;

push @my_main_array, {%my_hash};
%{$my_main_array[0]}{[@my_array]}++;

push @my_main_array, {%my_hash};
%{$my_main_array[1]}{[@my_array]}++;

Now, I am creating an anonymous hash initialized with %my_hash and pushing
it into @my_main_array. Then I am creating a new element in that hash with
the key being a reference to an anonymous array initialized with @my_array.

Then I repeat the process for the second element of @my_main_array.

Is it correct to use the '++' at the end to create a value for the hash?
Does this give it a value of one? I have seen this notation used commonly
with creating hash elements and I was not totally sure.

wana
 
W

wana

Brad said:
Brad Baxter wrote:
[snip]
On Fri, 5 Nov 2004, Tom wrote:
[snip]


@my_array;
%my_hash;
@my_main_array;

push @my_main_array, \%my_hash;
%{$my_main_array[0]}{\@my_array}++;

the hash value associated with key \@my_array would be 1 now, right? And
if I do:

push @my_main_array, \%my_hash;
%{$my_main_array[1]}{\@my_array}++;

I have a new hash as my second element of @my_main_array whose key is a
reference to a new array. In C++, this would not work the same because
all references to %my_hash and @my_array would just point to the same
hash and
array. Is this right? the concept seems counter-intuitive and can be a
little confusing.

wana

Are you the original poster? (i.e., Tom?)

Brad

No, just go by one name. I was just thinking out loud trying to work out
the problem for myself from what I know to see if I understood it and get
some help if I was wrong (which I am).

wana
 
W

wana

Eric said:
wana said:
@my_array;
%my_hash;
@my_main_array;

push @my_main_array, {%my_hash};
%{$my_main_array[0]}{[@my_array]}++;

Argh, to quote (possibly apocryphally) Niels Bohr: "That isn't right.
It's not even wrong!"

Read my reply again, closer. $hash{[@array]} does NOT use the
reference to the anonymous array as a key! Hashes ONLY use strings as
keys! What you're doing first stringifies the reference, and then
uses THAT as the key to the hash. What this means is threefold:

1) You can't turn a stringified reference back into a reference
(possibly you could, with XS, I'm not sure, but see below).
2) Even if you could, since stringifying a reference doesn't increment
its refcount, it's possible-- likely, even-- that the thing thus
referred to wouldn't exist any longer.
3) You don't appear to be interested in reading what I have to say, or
you would have seen that I addressed that very topic as the VERY
FIRST thing I commented on!

I don't like it first of all when I spend time reading the docs
that they have on their very own hard drive to someone, but I like
it even less when I do so and they ignore me!

If you want to get help from people on this newsgroup, read the
docs FIRST, and ask questions about them if necessary, and READ
what people write when they correct you!
Is it correct to use the '++' at the end to create a value for the hash?

That's perfectly fine; why souldn't it be?
Does this give it a value of one?

It increments whatever value was there; if it was 0, (or undef, which
is 0 when treated as a number), it becomes one, yes; what do you think
happens?
I have seen this notation used commonly
with creating hash elements and I was not totally sure.

Did you think of reading the documentation, which already exists on
your hard drive, before asking other people to read it for you?
Everything you've wanted to know so far has been readily answered in
the perlref, perlreftut, and perldata manpages. I don't mind
explaining things you've read there, and didn't understand, but when
it seems that you haven't read them at all, it's very frustrating.

-=Eric

sorry. i'll do some more in-depth reading this weekend. i am really trying
hard, but I can do better. thank you for your help.

wana :(
 
A

Arndt Jonasson

Eric Schwartz said:
Argh, to quote (possibly apocryphally) Niels Bohr: "That isn't right.
It's not even wrong!"

It seems to be more often attributed to Wolfgang Pauli.
 
T

Tom

Thanks thats a lot of information.
perldoc perldsc was quite useful.

Is it possible to store "AoA" - Array of Array as value to a hash key?

Array -> AoH -> AoA

I have an array and the individual element points to an element(in AoH).
Value of the hash key would be an AoA.
I'm trying this at my end. And would appreciate comments on this.
 
P

Paul Lalli

Tom said:
Thanks thats a lot of information.
perldoc perldsc was quite useful.

Is it possible to store "AoA" - Array of Array as value to a hash key?

Array -> AoH -> AoA

I have an array and the individual element points to an element(in AoH).
Value of the hash key would be an AoA.
I'm trying this at my end. And would appreciate comments on this.

Please do not top-post. That is, please post your reply *below* what
you are replying to. Thank you.

You can create data structures with arbitrarily large dimensions. Like
all multi-dimensional arrays, you cannot store an actual array, but you
instead store a reference to an array in your larger structure.

Thus, to create a Hash of Array of Arrays:

my %HoAoA = ( #the overall hash
foo => [ #value of hash at key 'foo'
#is an anonymous arrayref
[ 1, 2, 3, 4,], #first elem of this array is
#an anonymous array ref
[ 'a','b','c','d'], #etc . . .
],
bar => [
[ 10..14],
[ 'w'..'z'],
],
);


Using this structure:

%HoAoA is the entire Hash.
$HoAoA{foo} is a reference to an array of arrays
@{$HoAoA{foo}} is an array containing two array references
$HoAoA{foo}[0] is a reference to an array
@{$HoAoA{foo}[0]} is an array containing (1, 2, 3, 4)
$HoAoA{foo}[0][0] is the number 1

The standard module Data::Dumper is invaluable in figuring out exactly
what your structure looks like.

Hope this helps,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top