473,513 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OO Perl - An array as a class property

6 New Member
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 16117
KevinADC
4,059 Recognized Expert Specialist
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
pilsdumps
6 New Member
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 Recognized Expert Top Contributor
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

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

Similar topics

5
5754
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an...
3
1356
by: Nathan Sokalski | last post by:
I have an array declared as follows: Dim ButtonList() As NavButtonInfo and a Class defined as follows: Public Class NavButtonInfo Public Shared name As String
3
345
by: Mike R. | last post by:
I have declared two classes. The first class has 4 private variables. Each has a property defined. I'm calling a readfile sub from a second class. The second class also has an Arraylist...
7
1757
by: Ron | last post by:
Hello, I have 4 classes that use 4 DTS packages on 4 different tables. So I have Dim cls1 As New clsDTS1, cls2 As New clsDTS2 Dim cls3 As New clsDTS3, cls4 As New clsDTS4 Each class has a...
3
1403
by: Hugh O | last post by:
Hi, I am creating a class. I do not have any problem defining the property statements except when an array is involved. Would someone be able to show me the correct syntax for a Property...
0
9734
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
5
2638
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
6
18947
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
10
5559
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.