Using an Array in a Class

A

AJ

I want to have an array in a class I am making. I was testing out the
constructor by passing values to it, let it do its thing. Then call a
function that prints each class member out. Everything goes smooth
until I get to printing the array. It only prints the first member of
the array. I don't even think the constructor is setting the array
correctly the first time. I am new to perl, can someone help me.

Here is my class file:

package Router;

sub new {
my $Router = shift;
my ($name, $hCoord, $vCoord, $weight, @bob) = @_;

my $ref = {Name => $name, hCoord => $hCoord, vCoord => $vCoord,
weight => $weight, @{links} => @bob};

bless ($ref, $Router);
return $ref;
};

sub printShit{
my $ref = shift;
print "name: $ref->{'Name'}\n";
print "weight: $ref->{'weight'}\n";
print "hCoord: $ref->{'hCoord'}\n";
print "vCoord: $ref->{'vCoord'}\n";
print "LinkList: $ref->{'links'}\n";

}
1;


Here is my testFile that has a main:

use Router;

@names = ("hello", "1", "man");

$bob = new Router("wow", 1, 2, 3, @names);

$bob->printShit();
 
P

Paul Lalli

AJ said:
I want to have an array in a class I am making. I was testing out the
constructor by passing values to it, let it do its thing. Then call a
function that prints each class member out. Everything goes smooth
until I get to printing the array. It only prints the first member of
the array.

No it doesn't. It prints nothing about the array at all. At least, the
code you printed below doesn't. Please post the code you're actually
using.
I don't even think the constructor is setting the array
correctly the first time. I am new to perl, can someone help me.

Here is my class file:

package Router;

sub new {
my $Router = shift;
my ($name, $hCoord, $vCoord, $weight, @bob) = @_;

my $ref = {Name => $name, hCoord => $hCoord, vCoord => $vCoord,
weight => $weight, @{links} => @bob};

This is not what you want to do. What this is doing is adding an array
of elements, called @links, to the hash reference. The first element of
@links is the hash key for the 2nd element, the third a key for the
fourth, etc. Then it's adding @bob in the same manner.

An array cannot be a member of a hash. An array reference, however,
can:

my $ref = {
Name => $name,
hCoord => $hCoord,
vCoord => $vCoord,
weight => $weight,
links => \@bob,
};
bless ($ref, $Router);
return $ref;
};

sub printShit{
my $ref = shift;
print "name: $ref->{'Name'}\n";
print "weight: $ref->{'weight'}\n";
print "hCoord: $ref->{'hCoord'}\n";
print "vCoord: $ref->{'vCoord'}\n";
print "LinkList: $ref->{'links'}\n";

This will now have to be changed. You need to dereference the array:
print "LinkList: @{$ref->{'links'}}\n";
}
1;


Here is my testFile that has a main:

use Router;

@names = ("hello", "1", "man");

$bob = new Router("wow", 1, 2, 3, @names);

$bob->printShit();

For more information, read up on references. I reccomend starting with
perldoc perlreftut
and moving on eventually to
perldoc perllol
and
perldoc perldsc

Paul Lalli
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top