469,275 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

OO Perl - An array as a class property

I have a simple class to create an html table:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. package clTableMaker;
  4.  
  5. sub new 
  6. {
  7.   #constructor
  8.   my ($class_name, $width, $border, $cellspacing, $cellpadding) = @_;
  9.   my ($self) = {};
  10.   bless ($self, $class_name);
  11.  
  12.   my @label = ();
  13.   my @data = ();
  14.   $self->{'_label'} = @label; #assign empty list
  15.   $self->{'_data'} = @data;  #assign empty list
  16.  
  17.   return $self;
  18. }
  19.  
  20. sub add_row
  21. {
  22.   #add a row of data to the table
  23.   #fill the @label and @data
  24.  
  25.   push ($self->{'_label'}), $label);  #THIS DOES NOT WORK!
  26.   push ($self->('_data'), $data);    #THIS DOES NOT WORK!
  27. }
  28.  
To use this class I want to create a new object and initialise the table width etc (this works fine), then I want to be able to add 2-column rows like this:

Expand|Select|Wrap|Line Numbers
  1. my $table = clTableMaker->new();
  2. $table->add_row('this is label', 'this is data');
  3. $table->add_row('this is label2', 'this is data2');
  4.  
Once all rows have been added I will have a method to foreach the properties and generate an html row for each one.

The problem I'm having is that I can't get my head around how to add each row to the properties $self->{'_label'} and $self->{'_data'} using the add_row method. The pushes don't work and I'm assuming this is because I'm trying to push onto a scalar variable.

Can somebody show me how I might achieve this?

Thanks in advance,
Richard
Aug 28 '07 #1
3 15256
KevinADC
4,059 Expert 2GB
instead of:


Expand|Select|Wrap|Line Numbers
  1. my @label = ();
  2. my @data = ();
  3.  
  4. $self->{'_label'} = @label; #assign empty list
  5. $self->{'_data'} = @data;  #assign empty list
try:

Expand|Select|Wrap|Line Numbers
  1. $self->{'_label'} = []; #reference to an annonymous array
  2. $self->{'_data'} = []; #reference to an annonymous array
  3.  
then instead of:

Expand|Select|Wrap|Line Numbers
  1. push ($self->{'_label'}), $label);  
  2. push ($self->('_data'), $data);    
  3.  
try:

Expand|Select|Wrap|Line Numbers
  1. push (@{$self->{'_label'}}), $label);  
  2. push (@{$self->{'_data'}}), $data);
  3.  
Aug 28 '07 #2
Cheers that works perfectly.

Just needed to put in the extra bracket at the start:

Expand|Select|Wrap|Line Numbers
  1. push ((@{$self->{'_label'}}), $label);  
  2. push ((@{$self->{'_data'}}), $data);
  3.  
;)
Aug 29 '07 #3
miller
1,089 Expert 1GB
Or simplifying your code a little:

Expand|Select|Wrap|Line Numbers
  1. package clTableMaker;
  2.  
  3. use strict;
  4.  
  5. sub new {
  6.     #constructor
  7.     my ($class_name, $width, $border, $cellspacing, $cellpadding) = @_;
  8.  
  9.     my $self = bless {
  10.         _label => [],
  11.         _data  => [],
  12.     }, $class_name;
  13.  
  14.     return $self;
  15. }
  16.  
  17. sub add_row {
  18.     #add a row of data to the table
  19.     #fill the @label and @data
  20.  
  21.     push @{$self->{_label}}, $label;
  22.     push @{$self->{_data}}, $data;
  23. }
  24.  
Be sure to read perldsc for a deeper understanding of complex data structures.

- Miller
Aug 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by John Smith | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
3 posts views Thread by Mike R. | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
5 posts views Thread by Sam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.