Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sub returning reference

Reply
Thread Tools

Sub returning reference

 
 
Tony Muler
Guest
Posts: n/a
 
      01-09-2004
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.








 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      01-09-2004
On Fri, 09 Jan 2004 11:48:21 +0100, Tony Muler wrote:
> [...]
> Isn't it strange to create my @data in the method and
> return a reference to it.


Not at all. It's _good_ style, because you don't have to pass that,
possibly, huge amount of data around all the time.

Consider this snippet of code;

my @numbers = foo();

sub foo {
my @array = ();

for ( 1..10_000_000 ) {
push( @array, $_ );
}

return @array;
}

Both @array and @numbers would be quite huge, and when calling the
subroutine, the contents of @array would be copied to @numbers.

With references, ie. 'return \@array', only the _reference to_ the array
(ie. '@array') will be returned.


--
Tore Aursand <(E-Mail Removed)>
"The purpose of all war is ultimately peace." -- Saint Augustine
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      01-09-2004
Tony Muler <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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


You don't need the line count. If you did, you could use $.

> 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. ^^^^


The last two lines are better written as

push @data, [ split /-delimiter-/ ];

> }
> 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?


Not at all, it's standard and considered good practice.

> Couldn't I build an array (instead arrayref)


You *are* building an array. You are returning a reference to the
array you built. You could also return the array itself, at some
cost.

> of
> arrays (instead of arrayrefs) and return this,


No. "Arrays of arrays" in the true sense don't exist in Perl. The
values in arrays (and hashes) are always scalars, never aggregates,
so you must store references.

> so data would be called like
> print $content[2][3];


You can, syntactic sugar makes it so. But that's an abbreviation for
the explicit "$content->[2]->[3]".

> Or am I totally confused now? Are arrays of arrays
> always just arrays of array references?


Indeed. Same as in C, by the way, and some other languages.

> Thank you for any recommendations or style comments.


See perlref, perlreftut and perldsc.

Anno
 
Reply With Quote
 
Tony Muler
Guest
Posts: n/a
 
      01-09-2004
Thank you both for your help; for confirming
that my estimations were not so wrong and my code
was not soooo bad.


Anno Siegel wrote:
> You don't need the line count. If you did, you could use $.


Aha. Perl and its useful special variables. Didn't know this one.
( Ok, perldoc perlvar )

>> my @row = split (/-delimiter-/);
>> $data[$i++] = \@row; # $data[$i++]=@row not ok. Why?


> The last two lines are better written as
>
> push @data, [ split /-delimiter-/ ];


Perfect. I like that.

T.

 
Reply With Quote
 
Lukas Mai
Guest
Posts: n/a
 
      01-09-2004
Anno Siegel <(E-Mail Removed)-berlin.de> wrote:
> Tony Muler <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> so data would be called like
>> print $content[2][3];


> You can, syntactic sugar makes it so. But that's an abbreviation for
> the explicit "$content->[2]->[3]".


No, the arrow is optional between bracket subscripts, so
$content[2][3] is the same as $content[2]->[3] or ${$content[2]}[3].
It accesses the third element of @content.
$content->[2]->[3] is the same as $content->[2][3] or
${${$content}[2]}[3] and treats $content as an array reference.

HTH, Lukas
 
Reply With Quote
 
Gunnar Strand
Guest
Posts: n/a
 
      01-09-2004
Lukas Mai wrote:
> Anno Siegel <(E-Mail Removed)-berlin.de> wrote:
>
>>Tony Muler <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>
>>>so data would be called like
>>>print $content[2][3];

>
>
>>You can, syntactic sugar makes it so. But that's an abbreviation for
>>the explicit "$content->[2]->[3]".

>
>
> No, the arrow is optional between bracket subscripts, so
> $content[2][3] is the same as $content[2]->[3] or ${$content[2]}[3].
> It accesses the third element of @content.
> $content->[2]->[3] is the same as $content->[2][3] or
> ${${$content}[2]}[3] and treats $content as an array reference.


And, for completeness sake, it can also be written as $$content[2][3].

Kind Regards, /Gunnar

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
Possible to terminate a sub that loads another sub? Kathy Burke ASP .Net 3 11-24-2003 02:01 PM
Returning a reference to an existing C++ object as a reference JustMe Perl Misc 1 08-29-2003 07:02 AM
how do make a pop-up in sub ASP.net sub ? THY ASP .Net 1 08-18-2003 11:30 PM



Advertisments