Ok, I'm not sure if the above title made sense, here is what I am trying to do:

Suppose I am writing a module that looks something like this:

package Whatever;
use strict;
use warnings;

#constructor--will take one argument when called
sub new {
my $class = shift;
my $name = shift;
my $self = { name => $name, items => {[]}, };
return bless $self, $class;
}

My question is, how to put values into the 'items' portion, I have tried writing a method like this:

sub add {
	my $self = shift;
	my $key = shift;
	my @values = @_;

	%{ $self->{items} {$key } =  @values };
}

I know, because of assignment parameters, that the key to the hash will always be the first item of @_ (after the class name) . Can someone give me a hand with this?

line 6 above should be probably be written like this:

%{ $self->{items} {$key }} =  @values;

When you declare your new object initially you don't need the [] brackets in there:

my $self = { name => $name, items => {} };

It may or may not be a problem to have them in there, I'm not sure without testing code, but I am sure you can safely remove them.

Hello,

Thank you for your help. I have one more question, which then relates to outputing these items. (or more specifically, the 0th and 1st index of them)

sub show {
        my $self = shift;
        foreach my $key (keys %{ $self->{items} }) {
                print $key, " ", %{ $self->{items}{$key}->[0] }, " ", %{ $self->{items}{$key}->[1] }, "\n";
        }
}

But when I try to run this, it complains about it not being an array reference. I have tried it with and without the -> operator.

You don't need the %{} brackets to dereference individual parts of the hash of array. See if this does it:

sub show {
        my $self = shift;
        foreach my $key (keys %{ $self->{items} }) {
                print $key, " ", $self->{items}{$key}->[0], " ", $self->{items}{$key}->[1] , "\n";
        }
}

Hello,

I have tried the above suggested, which does not work; however, this does:

sub show {
        my $self = shift;
        foreach my $key (keys %{ $self->{items} }) {
                print $key, " ", %{  $self->{items}{$key} }, "\n";
        }
}

This, however, bothers me for two reasons. While it does produce the expected output, it complains about the use of an uninitialised value in the print statement. (It doesn't tell me which value it is though). Also, it bothers me because I think that, at some point later in the program, I will need to acess the values of the hash according to their index numbers.

try like this:

sub show {
        my $self = shift;
        foreach my $key (keys %{ $self->{items} }) {
                print $key, " ", $self->{items}->{$key}->[0], " ", $self->{items}->{$key}->[1] , "\n";
        }
}

note the extra -> in the print command between {items} and {$key}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.