Steeeeeve has asked for the wisdom of the Perl Monks concerning the following question:

There must be some way to do this properly:

my @TABLE_ARRAY = values ($self->{TABLE});

The error generated is:

Type of arg 1 to values must be hash (not hash element) at C:/InetPub/wwwroot/GREENTV2/library/Lady/Lady_TM.pm line 211, near "})"

I'm converting Lady TM making quite alot of progress on running it with the "strict" pragma but I'm stumped here....


-Steeeeeve

Replies are listed 'Best First'.
Re: getting values from a hash element???
by MeowChow (Vicar) on Feb 06, 2001 at 08:40 UTC
    $self->{TABLE} is a reference to a hash, but values expects an actual hash as its argument. You have to de-reference the hash reference:
    my @TABLE_ARRAY = values (%{$self->{TABLE}});
       MeowChow                                               
                    print $/='"',(`$^X\144oc $^X\146aq1`)[-2]
Re: getting values from a hash element???
by geektron (Curate) on Feb 06, 2001 at 08:43 UTC
    if you're working with a real hash, you need:
    my @T_A = values %table;
    if you're working with an anonymous hash, you need:
    my @T_A = values %{$self->{TABLE}};

    $self->{TABLE} is an element of an anonymous hash, with a structure presumably like the following:

    $self = { TABLE => $some var, METHOD => @some_other_var, ELEMENT => $some_third_var };
    $self->{TABLE} gets you the value of $some_var. you still need to dig into $some_var to get the hash elements.
(tye)Re: getting values from a hash element???
by tye (Sage) on Feb 06, 2001 at 08:54 UTC
Re: getting values from a hash element???
by ryddler (Monk) on Feb 06, 2001 at 09:16 UTC

    Without seeing the rest of your code, I'll venture this response.

    In the code you displayed here, $self->{TABLE} is a reference to a hash element, and if it's the hash being referenced by "$self" that you wish to retrieve the values from, then you merely need to dereference the hash like this:

    my @TABLE_ARRAY = values ( %{$self} );

    Which will get you back the values ("val1", "val2", "val3") from a structure that looks like this:

    $self = { "key1" => "val1", "key2" => "val2", "key3" => "val3", };

    If, on the other hand, $self->{TABLE} contains a reference to another hash as it's value, and that is what you're after, then you'll need to dereference it like this:

    my @TABLE_ARRAY = values ( %{$self->{TABLE}} );

    which will get you back the values ("val1", "val2", "val3") from a structure that looks like this:

    $self = { "TABLE" => { "key1" => "val1", "key2" => "val2", "key3" => "val3", } };

    Study up on perlref, and I would also highly recommend you take a look at Data::Dumper which lets you print them out and have a peek.

    ryddler