Updating an array within a hash

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

  1. #1

    Default Updating an array within a hash

    Hi Everybdy,
    I am stuck in a problem for which I need your help. My
    problem spins around adding an element in an array
    within a hash.
    I have a hash declared as $hash{"1"}=@array; now I
    want to add an element to it within a loop. How would
    I do this? I would be highly grateful to you if you
    could help me.

    Regards
    Soumyadeep

    __________________________________
    Do you Yahoo!?
    Yahoo! Finance: Get your refund fast by filing online.
    [url]http://taxes.yahoo.com/filing.html[/url]
    Soumyadeep Nandi Guest

  2. Similar Questions and Discussions

    1. 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...
    2. adding an array as a hash value
      Hi Dermot. Dermot Paikkos wrote: And use warnings; These variables need to have better names so that it's more
    3. Array and hash iteration questions
      I have a CSV file and I'm trying to do a few things with it. Essentially what it boils down to is: count the number of times a certain value is...
    4. Array and Hash to_s
      On Saturday, July 19, 2003, at 12:56 PM, Yukihiro Matsumoto wrote: While we're on the subject of "to_s" and similar friends, I have a question...
    5. How to put a filehandle into a hash array?
      I need to build an array of filehandles so different records go into different output files depending on a value. This is the code: $dt =...
  3. #2

    Default Re: Updating an array within a hash

    Soumyadeep nandi wrote:
    >Hi Everybdy,
    >I am stuck in a problem for which I need your help. My
    >problem spins around adding an element in an array
    >within a hash.
    >I have a hash declared as $hash{"1"}=@array; now I
    >want to add an element to it within a loop. How would
    >I do this? I would be highly grateful to you if you
    >could help me.
    Your syntax puts a number (the number of elements in @array into $hash{"1"}.

    I guess you want to set an array reference as the hash key's value:

    $hash{"1"} = \@array;

    In that case, you can just

    push @{$hash{"1"}}, $_ for (...list goes here...);

    to push into the array referenced by $hash{"1"}.

    Best,

    Jan
    --
    These are my principles and if you don't like them... well, I have others. - Groucho Marx
    Jan Eden Guest

  4. #3

    Default RE: Updating an array within a hash

    > Hi Everybdy,
    > I am stuck in a problem for which I need your help. My
    > problem spins around adding an element in an array
    > within a hash.
    > I have a hash declared as $hash{"1"}=@array; now I
    > want to add an element to it within a loop. How would
    > I do this? I would be highly grateful to you if you
    > could help me.
    Hi,
    I'm not sure you can just go "hash = array" as they're somewhat different, in the same way you can't go "scalar = array" (you can but it won't do what you think). But you can assign the hash the reference of the array. For example:
    $hash{1} = \@array;

    Or:
    push(@{$hash{1}}, @array);

    Then to loop through the array whose reference is assigned to $hash{1} you can do:
    foreach(@{$hash{1}})
    {
    # do something with $_
    }

    Cheers!

    __________________________________________________ ______________________
    This email has been scanned for all viruses by the MessageLabs Email
    Security System. For more information on a proactive email security
    service working around the clock, around the globe, visit
    [url]http://www.messagelabs.com[/url]
    __________________________________________________ ______________________
    Stephen Hardisty Guest

  5. #4

    Default Re: Updating an array within a hash

    Jan Eden wrote:
    >
    > Soumyadeep nandi wrote:
    >
    > >I am stuck in a problem for which I need your help. My
    > >problem spins around adding an element in an array
    > >within a hash.
    > >I have a hash declared as $hash{"1"}=@array; now I
    > >want to add an element to it within a loop. How would
    > >I do this? I would be highly grateful to you if you
    > >could help me.
    >
    > Your syntax puts a number (the number of elements in @array into $hash{"1"}.
    >
    > I guess you want to set an array reference as the hash key's value:
    >
    > $hash{"1"} = \@array;
    >
    > In that case, you can just
    >
    > push @{$hash{"1"}}, $_ for (...list goes here...);
    >
    > to push into the array referenced by $hash{"1"}.
    The second argument of push() is a list so you don't need a for loop
    there.

    push @{$hash{"1"}}, (...list goes here...);


    :-)
    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #5

    Default Re: Updating an array within a hash

    Hi Jan and Stephen,

    I could solve my problem. The way I could do is as
    follows:

    push (@{$C{$range}}, $c);

    where C is the hash having keys some integers and
    values as an array. So I could add more elements($c)
    to the array inside that hash(%C) is as above.

    Thank you so much Stephen and Jan for help and
    suggestions.



    __________________________________
    Do you Yahoo!?
    Yahoo! Finance: Get your refund fast by filing online.
    [url]http://taxes.yahoo.com/filing.html[/url]
    Soumyadeep Nandi 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