array reference expected

Discussion in 'Perl Misc' started by bnapus, Oct 29, 2003.

  1. bnapus

    bnapus Guest

    Perlers -

    I want to assign a value to an annonymous hash:
    $hash->[$i][$j]{Score} = 1;

    Why won't it accept a scalar? I get an error about perl expecting an
    address to an array. What I envisioned was a hash within a 2D array.

    I ended up using this:
    $hash->[$i]{$j}{Score} = 1;

    and it does what I want it to - just curious.

    Josh
     
    bnapus, Oct 29, 2003
    #1
    1. Advertising

  2. On Wed, 29 Oct 2003 10:50:50 -0800, bnapus wrote:

    > Perlers -
    >
    > I want to assign a value to an annonymous hash:
    > $hash->[$i][$j]{Score} = 1;
    >
    > Why won't it accept a scalar? I get an error about perl expecting an
    > address to an array. What I envisioned was a hash within a 2D array.
    >
    > I ended up using this:
    > $hash->[$i]{$j}{Score} = 1;
    >
    > and it does what I want it to - just curious.
    >
    > Josh


    Without seeing the rest of your code, I don't have an answer for you. But
    yes, it is possible to do what you want to do:
    perl -MData::Dumper -we '$hash->[0][0]{Score}=1;print Dumper $hash'

    I believe you are supposed to breakdown your problem to a simple script
    that shows the problem, then post that script. Often durring this process,
    you find the error yourself.
    - Brian
     
    Brian Harnish, Oct 29, 2003
    #2
    1. Advertising

  3. bnapus

    Ben Morrow Guest

    (bnapus) wrote:
    > I want to assign a value to an annonymous hash:
    > $hash->[$i][$j]{Score} = 1;


    This implies $hash is a
    reference to an array containing
    references to arrays containing
    references to hashes,
    i.e., that the structure you want is

    $hash = [
    ($i - 1 elements),
    [
    ($j - 1 elements),
    {
    Score => 1
    }
    ]
    ];

    > Why won't it accept a scalar? I get an error about perl expecting an
    > address to an array. What I envisioned was a hash within a 2D array.
    >
    > I ended up using this:
    > $hash->[$i]{$j}{Score} = 1;


    ....which implies that the structure you actually have is

    $hash = [
    ($i - 1 elements),
    {
    $j => {
    Score => 1
    }
    }
    ];

    .. The error about 'reference to an array' is referring to the contents
    of $hash->[$i]: by attempting to dereference it with [$j] you are
    assuming it contains an arrayref, when it seems as though it in fact
    contains a hashref.

    > and it does what I want it to - just curious.


    I don't think it does. For a start, the elements of $hash->[$i] are
    ordered in the structure you (thought you) wanted, whereas here they
    are not.

    I think you have misunderstood either your data structure or how
    references work in Perl. Add 'use Data::Dumper;' to the top of your
    script and 'print Dumper $hash;' just before this line. Then carefully
    re-read perllol and perlreftut again, and compare with what you see.

    If you still don't understand, then explain carefully what you are
    trying to achieve with this data structure and show the code you have
    at present, and someone will be better placed to tell you which one
    you meant and what is wrong with your code.

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    |----------------+---------------| The Levellers, 'Believers'
     
    Ben Morrow, Oct 29, 2003
    #3
    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. Replies:
    13
    Views:
    13,027
    Kai-Uwe Bux
    Jan 22, 2007
  2. Rohit
    Replies:
    9
    Views:
    463
    Keith Thompson
    Sep 18, 2008
  3. Dooza
    Replies:
    12
    Views:
    256
    Dooza
    Mar 11, 2008
  4. gga
    Replies:
    6
    Views:
    150
    Stefan Lang
    Feb 17, 2005
  5. Dolaameng

    Does Array.apply work as expected?

    Dolaameng, Jan 14, 2010, in forum: Javascript
    Replies:
    3
    Views:
    252
    Dolaameng
    Jan 14, 2010
Loading...

Share This Page