Re: improving my code: array of references



On Fri, 2007-04-27 at 12:03 -0400, Chas Owens wrote:
On 4/27/07, Pierre Mariani <frenchyp@xxxxxxxxx> wrote:
snip
- modify_variable() doesn't appear to modify anything, otherwise why
are you assigning its return value to the scalar passed as a parameter?
It seems to be just a function.

Modify_variable modifies its input variable.

I think the issue is what you mean by "modifies". In order to say
that the modify_variable function modifies its input then something
like the following must be true:

my $foo = 5
modify_variable($foo)
print "$foo\n"; #prints "6\n"


Yeah, that's exactly it.



- Why are you using references? Are you sure you need to?

Please correct me if I am wrong.
My understanding is that:
1) if I do:
my @array = ($a, $b, $c);
for (@array) { $_ = modify_variable($_)}
I am going to modify $array[0], $array[1] and $array[2], and NOT $a, $b,
$c.

Yes, that is correct, but this is most likely an invalid use of an
array. Either you should be using a array from the beginning, or you
should be using a list. Assigning a bunch of scalar variables to an
array just to group them is useless.

Agree on that, but given other details that I didn't communicate, it is
not quite possible (this is actually a pl/perlu function in postgresql,
the variables I am working on are passed to the function, and their type
is limited by the function definition, so unless I create a composite
type I can't pass them as an array directly).

$_ = modify_variable($_) for $a, $b, $c;

is a better construct. However, it would be even better to make
modify_variable take multiple arguments:

($a, $b, $c) = modify_variables($a, $b, $c);

I agree on that (even better would be to make it work on references),
but unfortunately I can't modify it.

2) if I do:
for ($a, $b, $c) {$_ = modify_variable($_)}
I am going to modify $a, $b, $c, which is good, but if $a, $b, $c are
big I am going to be passing around lots of data.

Ah, here is the crux of the problem. You thing you are passing
copies. Perl aliases its arguments to functions. In C++ terms you
are passing by reference (if I got the terminology right). Passing
"this is a really long string" and "a" both take up the same amount of
time and memory. Only passing around arrays and hashes take up O(n)
space and time. That is why it is good form to pass them as
references.

snip
$_ = function($_) foreach ($var1, $var2, $var3);

Will this do? Or is there more to the problem than you've explained?
snip

$_ = function($_) foreach ($var1, $var2, $var3);

is the same as

foreach ($var1, $var2, $var3) {
$_ = function($_)
}

If you only have one statement* in a loop for can write it in the
former method. It is considered by many to be clearer.


* well, you can chain statements using , but that is poor form outside
of Perl Golf, Obfuscation, and one-liners.

Thank you for the information. I was familiar with this kind of syntax
but never used it.

.



Relevant Pages

  • Re: How can this be?
    ... This is assigning an array variable to have the value of the list ... The shift() function returns ... modifies its argument. ...
    (comp.lang.perl.misc)
  • Re: improving my code: array of references
    ... Modify_variable modifies its input variable. ... Either you should be using a array from the beginning, ... are passing by reference. ...
    (perl.beginners)
  • Re: 3rd and Final Post OCX Design Question
    ... whether there would be any difference in speed hit by creating the array on ... the client and passing a reference to the array or passing the picture and ... >> What is the difference between passing a picture as IPictureDisp as ... > the reference to the object. ...
    (microsoft.public.vb.general.discussion)
  • Re: Proper use of delegates.
    ... the compiler will do it for you as long what you're passing doesn't look like an objectto the compiler. ... But any time you don't need to pass an array of parameters, yes...you could explicitly do the cast just to make sure that the overload you want is the one that's actually used. ... Then, what you're passing is always an object, with some number of elements equal to the number of parameters for the method. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Over 29 args in a UDF with double parens
    ... using the extra parenthesis will pass all references as an array to the ... else you could test against passing multiareas ... If you write a UDF 'to the max': ... > could somehow determine what cell references were passed. ...
    (microsoft.public.excel.programming)

Loading