T
Tony Muler
Hi there,
the following method (shortened for this post) looks
strange to me.
-- Code Start --
package DataFile;
# ....
sub get_data {
# Read data from a delimited file in an array or arrays
# (in which $data[a][x] means the value x in line a.
my $self = shift;
my $file = $self->{file};
my $i = 0; # Line count
my @row = ();
my @data = ();
open F, "<$file" || die "Cannot open $file. $!";
while (<F>) {
my @row = split (/-delimiter-/);
$data[$i++] = \@row; # $data[$i++]=@row not ok. Why?
# QUESTION TO C.L.P.M. ^^^^
}
return (\@data);
}
package test;
$obj = new DataFile;
$content = $obj->get_data();
print $content->[2]->[3]; # As an example. Ok.
-- Code End --
Isn't it strange to create my @data in the method and
return a reference to it. Ok, it stays alive because
something is pointing to it (right?), but isn't that
bad style?
Couldn't I build an array (instead arrayref) of
arrays (instead of arrayrefs) and return this,
so data would be called like
print $content[2][3];
Or am I totally confused now? Are arrays of arrays
always just arrays of array references?
Thank you for any recommendations or style comments.
T.
the following method (shortened for this post) looks
strange to me.
-- Code Start --
package DataFile;
# ....
sub get_data {
# Read data from a delimited file in an array or arrays
# (in which $data[a][x] means the value x in line a.
my $self = shift;
my $file = $self->{file};
my $i = 0; # Line count
my @row = ();
my @data = ();
open F, "<$file" || die "Cannot open $file. $!";
while (<F>) {
my @row = split (/-delimiter-/);
$data[$i++] = \@row; # $data[$i++]=@row not ok. Why?
# QUESTION TO C.L.P.M. ^^^^
}
return (\@data);
}
package test;
$obj = new DataFile;
$content = $obj->get_data();
print $content->[2]->[3]; # As an example. Ok.
-- Code End --
Isn't it strange to create my @data in the method and
return a reference to it. Ok, it stays alive because
something is pointing to it (right?), but isn't that
bad style?
Couldn't I build an array (instead arrayref) of
arrays (instead of arrayrefs) and return this,
so data would be called like
print $content[2][3];
Or am I totally confused now? Are arrays of arrays
always just arrays of array references?
Thank you for any recommendations or style comments.
T.