Array -> Reference to Hash of Arrays

Discussion in 'Perl Misc' started by Tom, Nov 5, 2004.

  1. Tom

    Tom Guest

    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 !
    Tom, Nov 5, 2004
    #1
    1. Advertising

  2. Tom

    Brad Baxter Guest

    On Fri, 5 Nov 2004, Tom wrote:

    > 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
    Brad Baxter, Nov 5, 2004
    #2
    1. Advertising

  3. Tom <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 5, 2004
    #3
  4. Tom wrote:

    > 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.
    Brian McCauley, Nov 5, 2004
    #4
  5. wana <> writes:
    > @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
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Nov 5, 2004
    #5
  6. Tom

    Brad Baxter Guest

    On Fri, 5 Nov 2004, wana wrote:

    > 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
    Brad Baxter, Nov 5, 2004
    #6
  7. Tom

    wana Guest

    Brad Baxter wrote:

    > On Fri, 5 Nov 2004, Tom wrote:
    >
    >> 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


    @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
    wana, Nov 5, 2004
    #7
  8. wana <> writes:
    > @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
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Nov 5, 2004
    #8
  9. Tom

    wana Guest

    Eric Schwartz wrote:

    > wana <> writes:
    >> @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
    wana, Nov 5, 2004
    #9
  10. Tom

    wana Guest

    Brad Baxter wrote:

    > On Fri, 5 Nov 2004, wana wrote:
    >
    >> 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
    wana, Nov 5, 2004
    #10
  11. Tom

    wana Guest

    Eric Schwartz wrote:

    > wana <> writes:
    >> @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 :(
    wana, Nov 5, 2004
    #11
  12. Eric Schwartz <> writes:
    > 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.
    Arndt Jonasson, Nov 8, 2004
    #12
  13. Tom

    Tom Guest

    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.


    Brian McCauley <> wrote in message news:<cmgcac$5ns$>...
    > Tom wrote:
    >
    > > 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.
    Tom, Nov 17, 2004
    #13
  14. Tom

    Paul Lalli Guest

    "Tom" <> wrote in message
    news:...
    > 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
    Paul Lalli, Nov 17, 2004
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  3. Anthony Martinez
    Replies:
    4
    Views:
    258
    Robert Klemme
    Jun 11, 2007
  4. Michal Suchanek
    Replies:
    6
    Views:
    214
    Nobuyoshi Nakada
    Jun 13, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    594
    David A. Black
    Jul 2, 2008
Loading...

Share This Page