Storing an Array in a Hash

Discussion in 'Perl' started by Hale, Dec 28, 2003.

  1. Hale

    Hale Guest

    Hi,

    I'm having a problem storing an array inside of a hash. I'm bringing
    in a hash via a reference and I want to assign a value to it. This is what
    I'm doing:


    push (@{$$node->{ELEMENTS}}, "New value");

    (I need to use "push" because this is going in a for-loop)

    However, when I run the script, I get the following:

    Can't use string ("0") as an ARRAY ref while "strict refs" in use at ......


    How do I solve this?

    THanks
    Hale, Dec 28, 2003
    #1
    1. Advertising

  2. Hale

    Kemper Guest

    Here is how I put values into a hash of arrays without push() that worked
    pretty good with a for loop

    $ar = [];

    %ref = ( 'ELEMENTS' , $ar );

    for ( $x=0; $x < 100; $x++ ) {

    $ref->{ELEMENTS}[$x] = "new Value";

    }

    You can get the reference like this:

    $arrayref = $ref->{ ELEMENT }

    push( @$arrayref, "new Element" ); # something like that if you need the
    push()


    "Hale" <> wrote in message
    news:uyFHb.30845$...
    > Hi,
    >
    > I'm having a problem storing an array inside of a hash. I'm bringing
    > in a hash via a reference and I want to assign a value to it. This is

    what
    > I'm doing:
    >
    >
    > push (@{$$node->{ELEMENTS}}, "New value");
    >
    > (I need to use "push" because this is going in a for-loop)
    >
    > However, when I run the script, I get the following:
    >
    > Can't use string ("0") as an ARRAY ref while "strict refs" in use at

    .......
    >
    >
    > How do I solve this?
    >
    > THanks
    >
    >
    Kemper, Dec 28, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    271
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    228
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    612
    David A. Black
    Jul 2, 2008
  5. Colvin
    Replies:
    3
    Views:
    168
    Colvin
    Dec 30, 2003
Loading...

Share This Page