baby code
I just realized even after years of dabbling in perl I don't
understand how function calls work. Can someone please be kind enough
to help me understand how to return multiple objects from a function
and use them? Here I'm interested in returning two objects: a hash,
and a list respectively. I'm sure there is more than one way of doing
this in perl
#!/usr/bin/perl
use strict;
sub func {
my %list;
$list{"map"} = "key";
$list{"l"}="j";
my [at] arr;
push ( [at] arr, "egg");
push ( [at] arr, "hell");
return \(%list, [at] arr);
}
my $arrref = &func();
my %l = $arrref->[0];
my [at] r = $arrref->[1];
print "keys\n";
foreach my $k(keys %l) { print "$k\n"; }
print "array\n";
foreach my $rr( [at] r) { print "$rr\n"; }
This prints:
vidura [at] localhost/tmp $ ./v.pl
keys
egg
array
hell
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: baby code
Below is one way to do it.
#!/usr/bin/perl
use strict;
sub func {
my %list;
$list{"map"} =3D "key";
$list{"l"}=3D"j";
my [at] arr;
push [at] arr,"egg";
push [at] arr,"hell";
return (\%list, \ [at] arr);
}
my ($hashref, $arrref) =3D &func();
print "keys\n";
foreach my $k(keys %{$hashref})
{
print "$hashref->{$k}\n";
}
print "array\n";
for my $i (0 .. $#{$arrref})
{
print "$arrref->[$i]\n";
}
Hope this clarifies.
Thanks,
Sachin Malesha
-----Original Message-----
From: K4 Monk [mailto:k4monk [at] gmail.com]
Sent: Tuesday, February 22, 2011 9:40 PM
To: beginners [at] perl.org
Subject: baby code
I just realized even after years of dabbling in perl I don't
understand how function calls work. Can someone please be kind enough
to help me understand how to return multiple objects from a function
and use them? Here I'm interested in returning two objects: a hash,
and a list respectively. I'm sure there is more than one way of doing
this in perl
#!/usr/bin/perl
use strict;
sub func {
my %list;
$list{"map"} =3D "key";
$list{"l"}=3D"j";
my [at] arr;
push ( [at] arr, "egg");
push ( [at] arr, "hell");
return \(%list, [at] arr);
}
my $arrref =3D &func();
my %l =3D $arrref->[0];
my [at] r =3D $arrref->[1];
print "keys\n";
foreach my $k(keys %l) { print "$k\n"; }
print "array\n";
foreach my $rr( [at] r) { print "$rr\n"; }
This prints:
vidura [at] localhost/tmp $ ./v.pl
keys
egg
array
hell
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: baby code
>>>>> "MS" == Malesha, Sachin <sachin.malesha [at] siemens.com> writes:
MS> Below is one way to do it.
if you are going to help, please cover most of the weaknesses of the
code and there are many.
MS> #!/usr/bin/perl
MS> use strict;
use warnings as well
MS> sub func {
poor sub name. names are important, even in test cases
MS> my %list;
MS> $list{"map"} = "key";
MS> $list{"l"}="j";
others have shown how to declare and initial in one statement. it should
be done that way.
MS> my [at] arr;
MS> push [at] arr,"egg";
MS> push [at] arr,"hell";
same here. that is wasteful and clunky code.
MS> return (\%list, \ [at] arr);
MS> }
MS> my ($hashref, $arrref) = &func();
others have said not to call subs with & so don't do it. there are
several reasons why.
MS> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
MS> For additional commands, e-mail: beginners-help [at] perl.org
MS> http://learn.perl.org/
MS> --
MS> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
MS> For additional commands, e-mail: beginners-help [at] perl.org
MS> http://learn.perl.org/
and you need to learn to bottom post and to trim quoted posts. i left
both footers to show you some of what you left behind.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/