Hashes of Hashes via subs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Holness

    Hashes of Hashes via subs

    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"}="arr ay1a";
    $array1{"1"}{"a "}{"Value"}="va lue1a";
    $array1{"1"}{"b "}{"Name"}="arr ay1b";
    $array1{"1"}{"b "}{"Value"}="va lue1b";

    $array1{"2"}{"a "}{"Name"}="arr ay2a";
    $array1{"2"}{"a "}{"Value"}="va lue2a";
    $array1{"2"}{"b "}{"Name"}="arr ay2b";
    $array1{"2"}{"b "}{"Value"}="va lue2b";

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

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

    # But I want to do it like this

    my %array2;

    $array2{"1"}=ge tSubArrays("1") ;
    $array2{"2"}=ge tSubArrays("2") ;

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


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

    #2
    Re: Hashes of Hashes via subs

    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"}="valu e".$_[0]."a";
    $tempArray{"b"} {"Name"}="array ".$_[0]."b";
    $tempArray{"b"} {"Value"}="valu e".$_[0]."b";
    return %tempArray;
    }

    Cheers,

    Ben

    Comment

    • Ben Holness

      #3
      Re: Hashes of Hashes via subs

      Worked out how to it :)

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

      Thanks for looking,

      Ben
      [color=blue]
      > # But I want to do it like this
      >
      > my %array2;
      >
      > $array2{"1"}={g etSubArrays("1" )};
      > $array2{"2"}={g etSubArrays("2" )};
      >
      > foreach my $level1 (keys %array2)
      > {
      > foreach my $level2 (keys %{$array2{$leve l1}})
      > {
      > print "Looking at $level1 : $level2. Name is ".$array2{$leve l1}{$level2}{"N ame"}."\n";
      > }
      > }
      >
      >
      > sub getSubArrays
      > {
      > my %tempArray;
      > $tempArray{"a"} {"Name"}="array ".$_[0]."a";
      > $tempArray{"b"} {"Name"}="array ".$_[0]."b";
      > return %tempArray;
      > }[/color]

      Comment

      • Roy Johnson

        #4
        Re: Hashes of Hashes via subs

        It is recommended that you post questions to comp.lang.perl. misc. This
        newsgroup is technically defunct.

        Comment

        • nobull@mail.com

          #5
          Re: Hashes of Hashes via subs

          In response to his own FAQ "Ben Holness" <usenet@bens-house.org.uk> wrote in message news:<pan.2003. 10.05.19.13.04. 532392@bens-house.org.uk>.. .
          [color=blue]
          > Worked out how to it :)[/color]

          TMTOWTDI!
          [color=blue]
          > It's as simple as curly brackets around the function calls to
          > getSubArrays!![/color]
          [color=blue]
          > $array2{"1"}={g etSubArrays("1" )};[/color]
          [color=blue]
          > sub getSubArrays
          > {
          > my %tempArray;[/color]
          # [snip!][color=blue]
          > return %tempArray;
          > }[/color]

          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.

          Comment

          • Ben Holness

            #6
            Re: Hashes of Hashes via subs

            > TMTOWTDI!

            Please can someone decipher for me? :)
            [color=blue]
            > Yes, but it is not efficient. See FAQ: "How can I [...] return a [...]
            > Hash [...]?"[/color]

            Thanks - I'll have a look
            [color=blue]
            > Oh, and please refrain from TOFU, it is considered rude - even when
            > following up yourself.[/color]

            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.
            [color=blue]
            > Also, it is confusing to alter quoted material without making it clear
            > you are doing so - even when quoting youself.[/color]

            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.
            [color=blue]
            > Others have pointed out this newsgroup does not exist. Please do not
            > start threads here.[/color]

            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

            Comment

            • nobull@mail.com

              #7
              Re: Hashes of Hashes via subs

              "Ben Holness" <usenet@bens-house.org.uk> wrote in message news:<pan.2003. 10.06.20.08.54. 877910@bens-house.org.uk>.. .[color=blue][color=green]
              > > TMTOWTDI![/color]
              >
              > Please can someone decipher for me? :)[/color]

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

              (Actually maybe it's written TIMTOWTDI).
              [color=blue][color=green]
              > > Oh, and please refrain from TOFU[/color][/color]
              [color=blue]
              > TOFU = Typing Own Follow Up?[/color]

              [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.
              [color=blue][color=green]
              > > Also, it is confusing to alter quoted material without making it clear
              > > you are doing so - even when quoting youself.[/color]
              >
              > 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.[/color]

              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.
              [color=blue][color=green]
              > > Others have pointed out this newsgroup does not exist. Please do not
              > > start threads here.[/color]
              >
              > 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 :)[/color]

              Curious - did you look at any of the threads before you posted? In
              most the status of the newsgroup is mentioned.
              [color=blue]
              > Apologies to anyone else that I have offended.[/color]

              I think "offended" would be too strong a word.
              [color=blue]
              > Lessons learnt![/color]

              That's what it's all about.

              Comment

              • Roy Johnson

                #8
                Re: Hashes of Hashes via subs

                "Ben Holness" <usenet@bens-house.org.uk> wrote in message news:<pan.2003. 10.06.20.08.54. 877910@bens-house.org.uk>.. .[color=blue][color=green]
                > > TMTOWTDI![/color]
                >
                > Please can someone decipher for me? :)[/color]

                There's More Than One Way To Do It. (the Perl motto)
                [color=blue]
                > TOFU = Typing Own Follow Up?[/color]

                Text over, fullquote under. That is, replying at the top, and
                including the entire original post below the reply.

                Comment

                • Ben Holness

                  #9
                  Re: Hashes of Hashes via subs

                  [color=blue][color=green]
                  >> 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 :)[/color]
                  >
                  > Curious - did you look at any of the threads before you posted? In
                  > most the status of the newsgroup is mentioned.[/color]

                  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

                  Comment

                  Working...