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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A table in an object

Status
Not open for further replies.

chirpyform

Programmer
Jun 20, 2003
202
FR
Hi,

I want to have a class perl that takes a line with a seperator ";" and stocks the result in a table that i can use later with getter and setter methodes.
ex
12;3;rrr;
would be stored in a table as
table[0]=12
table[1]=3
table[2]=rrr

I tried :

sub new
{
my ($class,$line,$sep) = @_;
my $this = {};
bless($this, $class);
print "sep : $sep \n line : $line";
$this->{sep} = $sep;
$this->{line} = $line;
$this->fields = split(/$this->{sep}/, $this->{line});
return $this;
}

sub getVar1()
{
my ($this) = @_;
return $this->fields[0];
}

but i have the impression that is not compatible :
Use of implicit split to @_ is deprecated at DRAP001_FENTREE1.pm line 13.
syntax error at DRAP001_FENTREE1.pm line 22, near "->fields["
syntax error at DRAP001_FENTREE1.pm line 33, near "->fields["
Compilation failed in require at DRAP001.pl line 3.
BEGIN failed--compilation aborted at DRAP001.pl line 3.

Can anybody help me please ?

Chris
 
Chris,
A had a similar problem. I'm not sure if this is what you are looking for or not..
Code:
    24 print "Enter age ex (18 65)\n";
     25 $age=<STDIN>;chomp $age;
     26 @agenum=split(/ /,$age);
     27 $lage=$agenum[0];
     28 $uage=$agenum[1];
I am new to perl, but this worked for my purposes. As I understand it...it splits the string $age into an array, which you can reference line by line. I think you would want to use '/;/' as the delimeter, where I used '/ /'.

-Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Thanks for the reply but this part is not the part that is difficult.
My problem in fact is to get back the value that i stored in the table.
ie
sub new
{
my ($class,$line,$sep) = @_;
my $this = {};
bless($this, $class);
print "sep : $sep \n line : $line";
$this->{sep} = $sep;
$this->{line} = $line;
$this->fields = split(/$this->{sep}/, $this->{line});
return $this;
}
works in the sense that the split returns a table of values.
The problem comes when I try and get the value $this->{fields}[0].
I want fields to be a class variable that stores all the different fields of my line. Like an object Java in fact.

Chris


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top