Hashes of Hashes via subs

Discussion in 'Perl' started by Ben Holness, Oct 5, 2003.

  1. Ben Holness

    Ben Holness Guest

    Hi All,

    I want to create a hash array, based on values in a database. Basically, I
    want a hash array for each database key and I want to use a sub to get the
    hash array, but I am having a great deal of difficulty!!

    I have written an example script, taking out the DB side of things, to
    explain what I want to do and how I want to do it. I am obviously doing
    something wrong, but I don't know what :)

    The end result (i.e. what is printed) needs to be:

    Looking at 1 : a. Name is array1a
    Looking at 1 : b. Name is array1b
    Looking at 2 : a. Name is array2a
    Looking at 2 : b. Name is array2b

    --

    Looking at 1 : a. Name is array1a
    Looking at 1 : b. Name is array1b
    Looking at 2 : a. Name is array2a
    Looking at 2 : b. Name is array2b

    but I only get the first set of printouts with the script as it is :(

    In this example, I have kept the foreach statement the same in both ways
    of doing it. I have tried accessing the hashes in a number of different
    ways, but without success.

    If anyone can point out what I am doing wrong, I would greatly appreciate
    it.

    Although I could re-write the actual code to work in a different way, I
    would prefer not to. Ideally, I would be able to get it working like I
    have laid out in this script.

    Many thanks,

    Ben

    --

    #!/usr/bin/perl

    # This is what I want:

    my %array1;
    $array1{"1"}{"a"}{"Name"}="array1a";
    $array1{"1"}{"a"}{"Value"}="value1a";
    $array1{"1"}{"b"}{"Name"}="array1b";
    $array1{"1"}{"b"}{"Value"}="value1b";

    $array1{"2"}{"a"}{"Name"}="array2a";
    $array1{"2"}{"a"}{"Value"}="value2a";
    $array1{"2"}{"b"}{"Name"}="array2b";
    $array1{"2"}{"b"}{"Value"}="value2b";

    foreach my $level1 (keys %array1)
    {
    foreach my $level2 (keys %{$array1{$level1}})
    {
    print "Looking at $level1 : $level2. Name is ".$array1{$level1}{$level2}{"Name"}."\n";
    }
    }

    print "\n--\n\n";

    # But I want to do it like this

    my %array2;

    $array2{"1"}=getSubArrays("1");
    $array2{"2"}=getSubArrays("2");

    foreach my $level1 (keys %array2)
    {
    foreach my $level2 (keys %{$array2{$level1}})
    {
    print "Looking at $level1 : $level2. Name is ".$array2{$level1}{$level2}{"Name"}."\n";
    }
    }


    sub getSubArrays
    {
    my %tempArray;
    $tempArray{"a"}{"Name"}="array".$_[0]."a";
    $tempArray{"b"}{"Name"}="array".$_[0]."b";
    return %tempArray;
    }
     
    Ben Holness, Oct 5, 2003
    #1
    1. Advertising

  2. Ben Holness

    Ben Holness Guest

    Just noticed a small error in the getSubArrays subroutine - I forgot to
    put in the values :)

    sub getSubArrays
    {
    my %tempArray;
    $tempArray{"a"}{"Name"}="array".$_[0]."a";
    $tempArray{"a"}{"Value"}="value".$_[0]."a";
    $tempArray{"b"}{"Name"}="array".$_[0]."b";
    $tempArray{"b"}{"Value"}="value".$_[0]."b";
    return %tempArray;
    }

    Cheers,

    Ben
     
    Ben Holness, Oct 5, 2003
    #2
    1. Advertising

  3. Ben Holness

    Ben Holness Guest

    Worked out how to it :)

    It's as simple as curly brackets around the function calls to
    getSubArrays!!

    Thanks for looking,

    Ben

    > # But I want to do it like this
    >
    > my %array2;
    >
    > $array2{"1"}={getSubArrays("1")};
    > $array2{"2"}={getSubArrays("2")};
    >
    > foreach my $level1 (keys %array2)
    > {
    > foreach my $level2 (keys %{$array2{$level1}})
    > {
    > print "Looking at $level1 : $level2. Name is ".$array2{$level1}{$level2}{"Name"}."\n";
    > }
    > }
    >
    >
    > sub getSubArrays
    > {
    > my %tempArray;
    > $tempArray{"a"}{"Name"}="array".$_[0]."a";
    > $tempArray{"b"}{"Name"}="array".$_[0]."b";
    > return %tempArray;
    > }
     
    Ben Holness, Oct 5, 2003
    #3
  4. Ben Holness

    Roy Johnson Guest

    It is recommended that you post questions to comp.lang.perl.misc. This
    newsgroup is technically defunct.
     
    Roy Johnson, Oct 6, 2003
    #4
  5. Ben Holness

    Guest

    In response to his own FAQ "Ben Holness" <> wrote in message news:<>...

    > Worked out how to it :)


    TMTOWTDI!

    > It's as simple as curly brackets around the function calls to
    > getSubArrays!!


    > $array2{"1"}={getSubArrays("1")};


    > sub getSubArrays
    > {
    > my %tempArray;

    # [snip!]
    > return %tempArray;
    > }


    Yes, but it is not efficient. See FAQ: "How can I [...] return a [...]
    Hash [...]?"

    Oh, and please refrain from TOFU, it is considered rude - even when
    following up yourself.

    Also, it is confusing to alter quoted material without making it clear
    you are doing so - even when quoting youself.

    Others have pointed out this newsgroup does not exist. Please do not
    start threads here.
     
    , Oct 6, 2003
    #5
  6. Ben Holness

    Ben Holness Guest

    > TMTOWTDI!

    Please can someone decipher for me? :)

    > Yes, but it is not efficient. See FAQ: "How can I [...] return a [...]
    > Hash [...]?"


    Thanks - I'll have a look

    > Oh, and please refrain from TOFU, it is considered rude - even when
    > following up yourself.


    TOFU = Typing Own Follow Up?
    Sorry - just wanted to (a) let any other interested parties know the
    answer (even if it turned out not to be the best one, but they would know
    that too now :) ) and (b) it means that other people know the problem has
    been solved and don't need to waste their time looking at it.

    > Also, it is confusing to alter quoted material without making it clear
    > you are doing so - even when quoting youself.


    I don't usually, it's just that the sub had changed so it shouldn't be all
    quoted, and it didn't look right with only some of it quoted.

    > Others have pointed out this newsgroup does not exist. Please do not
    > start threads here.


    This was my first post in this newsgroup. I was not aware of the status of
    it but I am now, so I wont start any new threads here :)

    Apologies to anyone else that I have offended. Lessons learnt!

    Cheers

    Ben
     
    Ben Holness, Oct 6, 2003
    #6
  7. Ben Holness

    Guest

    "Ben Holness" <> wrote in message news:<>...
    > > TMTOWTDI!

    >
    > Please can someone decipher for me? :)


    There's more than one way to do it. (A motto of the Perl community).

    (Actually maybe it's written TIMTOWTDI).

    > > Oh, and please refrain from TOFU


    > TOFU = Typing Own Follow Up?


    [new] Text Over, Full-quote Under. Quoted material should be
    interspersed with new material so as to give context the the new
    matierial that follows it. Only material needed to give context
    should be included.

    > > Also, it is confusing to alter quoted material without making it clear
    > > you are doing so - even when quoting youself.

    >
    > I don't usually, it's just that the sub had changed so it shouldn't be all
    > quoted, and it didn't look right with only some of it quoted.


    I sympathise but it did cause me confusion. You appeared to be saying
    you'd found a solution to the quoted problem - but the quoted material
    didn't illustrate the problem! Better to simply unquote the lot
    IMNSHO.

    > > Others have pointed out this newsgroup does not exist. Please do not
    > > start threads here.

    >
    > This was my first post in this newsgroup. I was not aware of the status of
    > it but I am now, so I wont start any new threads here :)


    Curious - did you look at any of the threads before you posted? In
    most the status of the newsgroup is mentioned.

    > Apologies to anyone else that I have offended.


    I think "offended" would be too strong a word.

    > Lessons learnt!


    That's what it's all about.
     
    , Oct 7, 2003
    #7
  8. Ben Holness

    Roy Johnson Guest

    "Ben Holness" <> wrote in message news:<>...
    > > TMTOWTDI!

    >
    > Please can someone decipher for me? :)


    There's More Than One Way To Do It. (the Perl motto)

    > TOFU = Typing Own Follow Up?


    Text over, fullquote under. That is, replying at the top, and
    including the entire original post below the reply.
     
    Roy Johnson, Oct 7, 2003
    #8
  9. Ben Holness

    Ben Holness Guest


    >> This was my first post in this newsgroup. I was not aware of the status of
    >> it but I am now, so I wont start any new threads here :)

    >
    > Curious - did you look at any of the threads before you posted? In
    > most the status of the newsgroup is mentioned.


    No - I searched on Google for other people asking the same question and I
    skim read the headers, but given that there 300 odd posts in the last
    month, it never occurred to me that this newsgroup was not supposed to be
    used :)

    Cheers,

    Ben
     
    Ben Holness, Oct 8, 2003
    #9
    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. Bolin

    Subs and lists

    Bolin, Nov 2, 2003, in forum: Perl
    Replies:
    6
    Views:
    578
  2. Showjumper
    Replies:
    0
    Views:
    4,438
    Showjumper
    Jun 26, 2003
  3. nakhi
    Replies:
    2
    Views:
    402
    nakhi
    Aug 22, 2003
  4. Steffen Loringer

    Useless variables/Subs checking

    Steffen Loringer, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    357
    Rob Meade
    Jan 30, 2004
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    233
Loading...

Share This Page