hash of arrays question

Discussion in 'Perl' started by John Gregory, Jun 11, 2006.

  1. John Gregory

    John Gregory Guest

    I'm new to perl and have a question. The perl book
    says that hashes map keys to values. Using the reverse
    function you can map values to keys. My problem is
    I have a hash of arrays and I want to map an array
    of values to a particular key. Is there any way to
    do this?

    Thanks,

    John
     
    John Gregory, Jun 11, 2006
    #1
    1. Advertisements

  2. John Gregory

    Skeleton Man Guest

    >I'm new to perl and have a question. The perl book
    >says that hashes map keys to values. Using the reverse
    >function you can map values to keys. My problem is
    >I have a hash of arrays and I want to map an array
    >of values to a particular key. Is there any way to
    >do this?


    There are two ways to do this:

    $sample{'key1'} = ['item1', 'item2', 'item3']; # Array of values mapped to a
    single hash key (note the use of square brackets [])
    print $sample{'key1'}[0]; # Prints "item1"

    Or:

    @data = ('item4, 'item5', 'item6', 'item7'); # Create a new array
    $sample{'key2'} = \@data; # Take a reference to the
    array (backslash operator), you could use any existing array here too.
    print $sample{'key2'};


    Let me know if you need anything explained a little more clearly :)

    Regards,
    Chris
     
    Skeleton Man, Jun 12, 2006
    #2
    1. Advertisements

  3. John Gregory

    mzi Guest

    Skeleton Man wrote:

    >>I'm new to perl and have a question. The perl book
    >>says that hashes map keys to values. Using the reverse
    >>function you can map values to keys. My problem is
    >>I have a hash of arrays and I want to map an array
    >>of values to a particular key. Is there any way to
    >>do this?

    >
    > There are two ways to do this:
    >
    > $sample{'key1'} = ['item1', 'item2', 'item3']; # Array of values mapped to
    > a single hash key (note the use of square brackets [])
    > print $sample{'key1'}[0]; # Prints "item1"
    >
    > Or:
    >
    > @data = ('item4, 'item5', 'item6', 'item7'); # Create a new array
    > $sample{'key2'} = \@data; # Take a reference to the
    > array (backslash operator), you could use any existing array here too.
    > print $sample{'key2'};
    >
    >
    > Let me know if you need anything explained a little more clearly :)


    This means: hash values can be references.

    --
    mzi
     
    mzi, Jun 12, 2006
    #3
  4. > I'm new to perl and have a question. The perl book
    > says that hashes map keys to values. Using the reverse
    > function you can map values to keys.


    AFAIK, the reverse function takes a list/array, and returns
    it in the opposite order.



    > My problem is
    > I have a hash of arrays and I want to map an array
    > of values to a particular key. Is there any way to
    > do this?


    Something like this?

    @contents = (1,2,3,4,5)
    $hashofarrays{key} = \@contents

    I can then reference like follows:

    print $hashofarrays{key}[2]

    outputs: 3
     
    Michael Gaylord, Sep 27, 2006
    #4
  5. John Gregory

    joesmith

    Joined:
    Aug 7, 2008
    Messages:
    1
    Likes Received:
    0
    Joe

    Hi

    I tried this output but didn't print

    $sample{key1} =(1,2,3,4,5);
    print $sample{key1}[0]; - it doesn't print the output of 1.


    any idea why ?
     
    joesmith, Aug 7, 2008
    #5
    1. Advertisements

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,694
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    986
    red floyd
    Nov 10, 2011
  3. p-hawk
    Replies:
    3
    Views:
    276
    Erik Veenstra
    Jul 6, 2006
  4. Adam Akhtar
    Replies:
    5
    Views:
    913
    Adam Akhtar
    Mar 25, 2008
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    1,083
    David A. Black
    Jul 2, 2008
  6. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    982
    Älphä Blüë
    Jul 18, 2009
  7. Tore Aursand
    Replies:
    3
    Views:
    820
    Anno Siegel
    Sep 16, 2003
  8. Lynn

    Accessing Hash of hash of arrays

    Lynn, Feb 14, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    813
    Alan J. Flavell
    Feb 16, 2005
Loading...