Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Attribute

Status
Not open for further replies.

dalhousi

Programmer
Mar 26, 2007
21
CA
Hi Everyone,

I'm trying to write a class in Perl. One of the attributes is an array.

How can I get/set the values of the array? Usually, I get the value of other attributes using:
$obj->{att} = "something";

but now, since it is an array, how can I do that? Here is the code I wrote:
Code:
package class_name;
use strict;

my @my_array;
1;
   sub new {
        my $self = {};
	$self->{my_array} = undef;
        bless($self);
        return $self;
   }

Then, when I try to assign some values to this array, it does not work:
Code:
$obj->{@my_array} = @another_array;
print $obj->{@my_array[2]};

Any suggestions or comments?

Thanks in advance
 
try:

Code:
$self->{my_array} = [];



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for the reply Kevin.

Unfortunately, it didn't work. Is my syntax correct? I mean I am not sure that the proper way to deal with arrays within a class is $obj->{@my_array} or $obj->{@my_array[2]}.

Cheers,
 
Hi,
I am not sure about the position of 1 in your package
Does it have to be at the end?
Try this..
Code:
package class_name;
use strict;

my @my_array;

   sub new {
        my $self = {};
        $self->{my_array} = undef;
        bless($self);
        return $self;
   }
1;
$obj->{@my_array} = @another_array;
print $obj->{$my_array[2]};

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
No, Still not working.

It seems to me that when I assign @another_array to $obj->{@my_array}, it assigns only the length of the array. So, if I try to print $obj->{$my_array[0]}, it will print the length of the array.
 
What about,
Code:
$obj->{@my_array} = [b]"[/b]@another_array[b]"[/b] ;

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Well, this works but in this case, $obj->{@my_array} will be a string instead of an array. Is there any other way of dealing with it as an array?

Thanks,
 
Code:
package class_name;
use strict;

   sub new {
        my $self = {};
        $self->{my_array} = [];
        bless($self);
        return $self;
   }
1;



$obj->{my_array} = \@another_array;.
print $obj->{my_array}->[2];

some tutorials here that should help you:


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin,
That worked, finally :)
I suppose that the "\" in Perl means reference, right?
One more thing: in order to copy the length of the array, the following should be added:
Code:
$obj->{my_array} = \@another_array;
$obj->{@my_array} = @another_array;
Otherwise, I could not get the length of the array.

Thanks again
 
Yes, the \ is a reference to the array. You can do it differently:


$obj->{my_array} = [@another_array];

which creates a copy of @another_array instead of a reference back to the original array.

This might be misleadig you:

$obj->{@my_array} = @another_array;

@my_array is just the name of the hash key, the '@' symbol has no meta meaning in that context. You are just assigning the value of an array to a scalar, which as you know returns the length of the array. It might be more appropriate to use a meaningful hash key name:

$obj->{array_length} = @another_array;






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
To assign to the array, simply dereference it like this:

Code:
@{$obj->{my_array}} = @another_array;

To determine the length of the array, simply use it in a scalar context.

Code:
my $length = scalar @{$obj->{my_array}};

Don't bother creating a secondary variable just to determine the length, as perl does that just fine for you.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top