multidimentional hash that has both two and three keys

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

  1. #1

    Default multidimentional hash that has both two and three keys

    Can you have a multidimentional hash that has both two and three keys?

    For example:

    Array with two keys and a value:
    $HASH-> {$VAR}{letter} ="a";

    Same Array with three keys and a value:
    $HASH->{$VAR}{$number){float} = "1.1";
    $HASH->{$VAR} {$number}{integer}="2";

    Is this possible? I'm not sure if it didn't wok b/c of an error in syntax or
    if it's because it's not possible.

    To print the two keys and value I know I do:

    print $HASH->{$VAR}{letter};


    BUT, if I have three keys and a value, how do I print it? If $number is a
    different number every time (1 ..10), how do I get it to print so that it
    iterates through each number and gives me the float and the integer for
    each.

    Hope this makes sense! Any examples would help get me started

    Thanx!

    __________________________________________________ _______________
    High-speed users—be more efficient online with the new MSN Premium Internet
    Software. [url]http://join.msn.com/?pgmarket=en-us&page=byoa/prem&ST=1[/url]

    Trina Espinoza Guest

  2. Similar Questions and Discussions

    1. DBI::mysql column names as hash keys?
      Hello, Given a DBI::mysql database with 2 tables and 5 columns like so: human --> name --> age --> sex dog --> breed --> colour
    2. 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 = {...
    3. Detecting duplicate keys in a hash I am "requiring"
      Hi, I have a configuration file which is in a perl hash (of hashes and arrays) format. $hash = { key1 => { key2 => val2, key3 => val3, },...
    4. 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......
    5. Using slices with 'my' to initialize hash keys and values.
      I have the following piece of code which works (meaning, does what I expect): #!/usr/bin/perl use strict; use warnings; my %translation;
  3. #2

    Default Re: multidimentional hash that has both two and three keys

    Trina Espinoza wrote:
    >
    > Can you have a multidimentional hash that has both two and three keys?
    >
    > For example:
    >
    > Array with two keys and a value:
    > $HASH-> {$VAR}{letter} ="a";
    >
    > Same Array with three keys and a value:
    > $HASH->{$VAR}{$number){float} = "1.1";
    That closing ')' should be a '}'.
    > $HASH->{$VAR} {$number}{integer}="2";
    >
    > Is this possible? I'm not sure if it didn't wok b/c of an error in syntax or
    > if it's because it's not possible.
    Yes, it's certainly possible. What error did you get?
    >
    > To print the two keys and value I know I do:
    >
    > print $HASH->{$VAR}{letter};
    >
    > BUT, if I have three keys and a value, how do I print it? If $number is a
    > different number every time (1 ..10), how do I get it to print so that it
    > iterates through each number and gives me the float and the integer for
    > each.
    Something like this, below?

    use strict;
    use warnings;

    my $HASH = {};
    my $VAR = 'key';
    my $number = 99;

    $HASH->{$VAR}{letter} = "a";
    $HASH->{$VAR}{$number}{float} = "1.1";
    $HASH->{$VAR}{$number}{integer} = "2";

    my $hash = $HASH->{$VAR};

    foreach my $key (keys %$hash) {

    my $value = $hash->{$key};

    if ($key eq 'letter') {

    printf "%s => letter %s\n", $key, $value;
    }
    else { # $key is a hash reference

    printf "number %s => float %s, integer %s\n",
    $key, $value->{float}, $value->{integer};
    }
    }

    **OUTPUT

    letter => letter a
    number 99 => float 1.1, integer 2

    > Hope this makes sense! Any examples would help get me started
    I'm not completely clear on your problem, but I hope this gets you going.

    Cheers,

    Rob


    Rob Dixon 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