Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Hash of Hash

    Greetings,

    I am attempting to make a hash of hashes or something equivalent but can't
    seem to get it working properly. Here is what I have so far:

    @name = ("one", "two", "three");

    Foreach my $layer (@name) {
    $thief_opt = {
    $layer = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    }
    };
    }

    ##--for test purposes simply print the vars for now

    while (($key, $val) = each %$thief_opt) {
    while (($key2, $val2) = each %$val) {
    print "$key $key2 $val2;
    }
    }


    I am trying to create a HoH that is called %thief_opt and who's first key is
    based on a list contained in an array called @name. This bit of code seems
    to work and create a HoH with the structure I want, but the problem is that
    every iteration of my foreach loop steps on the previous values for
    %thief_opt so that in the end I only get a HoH for the last value in @name
    (which in this case is "three").
    To try and explain it another way, I have a list inside of @name that
    might be something like: one, two, three. I want to make a HoH that would
    look more like the following if the values of @name were constants and could
    be hard coded:

    $thief_opt = {
    one = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    two = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    three = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    }

    I would appreciate any ideas or suggestions.

    Best Regards,

    Robert Zielfelder









    ..-. --..
    Robert Zielfelder Guest

  2. Similar Questions and Discussions

    1. Hash
      Hi, How set up functions hash in php4.3.2 thanks _________________________________________________________________ Charla con tus amigos en...
    2. Hash in AS 2.0
      So I was about to write a hash algorythm for flash and it appears brandon hall beat me by 2 years. Since there isn't anything here, in the flash...
    3. hash of hash of array slices
      This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't...
    4. Sort a hash based on values in the hash stored as arrays of hashes
      Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {...
    5. Another reference question (hash of hash references)
      beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes......
  3. #2

    Default RE: Hash of Hash

    I think you mean this...

    use strict;
    my %thief_opt;
    my @name = ("one", "two", "three");

    foreach my $layer (@name) {
    $thief_opt{$layer} = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    };
    }

    # test with this. prints out structure to console.
    use Data::Dumper;
    print Dumper \%thief_opt;

    The above prints this...

    $VAR1 = {
    'one' => {
    'origin' => 'datum',
    'use_arcs' => 'yes',
    'type' => 'solid'
    },
    'three' => {
    'origin' => 'datum',
    'use_arcs' => 'yes',
    'type' => 'solid'
    },
    'two' => {
    'origin' => 'datum',
    'use_arcs' => 'yes',
    'type' => 'solid'
    }
    };

    Rob



    -----Original Message-----
    From: Zielfelder, Robert [mailto:robert.zielfelder@tycoelectronics.com]
    Sent: Monday, August 18, 2003 9:38 AM
    To: Perl Beginners List (E-mail)
    Subject: Hash of Hash


    Greetings,

    I am attempting to make a hash of hashes or something equivalent but can't
    seem to get it working properly. Here is what I have so far:

    @name = ("one", "two", "three");

    Foreach my $layer (@name) {
    $thief_opt = {
    $layer = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    }
    };
    }

    ##--for test purposes simply print the vars for now

    while (($key, $val) = each %$thief_opt) {
    while (($key2, $val2) = each %$val) {
    print "$key $key2 $val2;
    }
    }


    I am trying to create a HoH that is called %thief_opt and who's first key is
    based on a list contained in an array called @name. This bit of code seems
    to work and create a HoH with the structure I want, but the problem is that
    every iteration of my foreach loop steps on the previous values for
    %thief_opt so that in the end I only get a HoH for the last value in @name
    (which in this case is "three").
    To try and explain it another way, I have a list inside of @name that
    might be something like: one, two, three. I want to make a HoH that would
    look more like the following if the values of @name were constants and could
    be hard coded:

    $thief_opt = {
    one = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    two = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    three = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    }

    I would appreciate any ideas or suggestions.

    Best Regards,

    Robert Zielfelder









    ..-. --..

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Rob Hanson Guest

  4. #3

    Default RE: Hash of Hash

    This is exactly what I was after. Not being a "real" programmer I was
    tripping on syntax as usual. Thanks for the help

    Rz


    ..-. --..
    -----Original Message-----
    From: Hanson, Rob [mailto:rhanson@aptegrity.com]
    Sent: Monday, August 18, 2003 9:50 AM
    To: Zielfelder, Robert; Perl Beginners List (E-mail)
    Subject: RE: Hash of Hash

    I think you mean this...

    use strict;
    my %thief_opt;
    my @name = ("one", "two", "three");

    foreach my $layer (@name) {
    $thief_opt{$layer} = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    };
    }

    # test with this. prints out structure to console.
    use Data::Dumper;
    print Dumper \%thief_opt;

    The above prints this...

    $VAR1 = {
    'one' => {
    'origin' => 'datum',
    'use_arcs' => 'yes',
    'type' => 'solid'
    },
    'three' => {
    'origin' => 'datum',
    'use_arcs' => 'yes',
    'type' => 'solid'
    },
    'two' => {
    'origin' => 'datum',
    'use_arcs' => 'yes',
    'type' => 'solid'
    }
    };

    Rob



    -----Original Message-----
    From: Zielfelder, Robert [mailto:robert.zielfelder@tycoelectronics.com]
    Sent: Monday, August 18, 2003 9:38 AM
    To: Perl Beginners List (E-mail)
    Subject: Hash of Hash


    Greetings,

    I am attempting to make a hash of hashes or something equivalent but can't
    seem to get it working properly. Here is what I have so far:

    @name = ("one", "two", "three");

    Foreach my $layer (@name) {
    $thief_opt = {
    $layer = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    }
    };
    }

    ##--for test purposes simply print the vars for now

    while (($key, $val) = each %$thief_opt) {
    while (($key2, $val2) = each %$val) {
    print "$key $key2 $val2;
    }
    }


    I am trying to create a HoH that is called %thief_opt and who's first key is
    based on a list contained in an array called @name. This bit of code seems
    to work and create a HoH with the structure I want, but the problem is that
    every iteration of my foreach loop steps on the previous values for
    %thief_opt so that in the end I only get a HoH for the last value in @name
    (which in this case is "three").
    To try and explain it another way, I have a list inside of @name that
    might be something like: one, two, three. I want to make a HoH that would
    look more like the following if the values of @name were constants and could
    be hard coded:

    $thief_opt = {
    one = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    two = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    three = {
    type => "solid",
    origin => "datum",
    use_arcs => "yes",
    },
    }

    I would appreciate any ideas or suggestions.

    Best Regards,

    Robert Zielfelder









    ..-. --..

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Robert Zielfelder Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139