hash of arrays question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Gregory

    hash of arrays question

    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
  • Skeleton Man

    #2
    Re: hash of arrays question

    >I'm new to perl and have a question. The perl book[color=blue]
    >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?[/color]

    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




    Comment

    • mzi

      #3
      Re: hash of arrays question

      Skeleton Man wrote:
      [color=blue][color=green]
      >>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?[/color]
      >
      > 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 :-)[/color]

      This means: hash values can be references.

      --
      mzi

      Comment

      • Michael Gaylord

        #4
        Re: hash of arrays question

        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{k ey} = \@contents

        I can then reference like follows:

        print $hashofarrays{k ey}[2]

        outputs: 3


        Comment

        Working...