Re: space saving wanted!
by broquaint (Abbot) on Apr 23, 2003 at 09:30 UTC
|
I wondered if there was a neat way of foreaching through a list of varibles that are names sequentially
There is, but don't do it. That may sound harsh, but use of symbolic variables is almost categorically a bad thing as a certain Mr. Dominus illustrates in Why it's stupid to `use a variable as a variable name' (with follow up articles here and here).
The alternatives are either creating an array instead of dynamically generating variables, or just use a hash e.g
## using an array
my @calcs = some_function();
for(@calcs[0 .. 3]) { ... }
## using a hash
for my $k (map "calc$_", 1 .. 4) {
do_stuff( $calc{$k} );
...
}
See. perldata for info on array slicing and the map docs for info on map.
HTH
_________ broquaint | [reply] [Watch: Dir/Any] [d/l] |
|
Another on-topic link on MJD's site well worth reading are the notes on his Perl Program Repair Shop and Red Flags talk and the O'ReillyNet article on the same subject.
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature
| [reply] [Watch: Dir/Any] |
Re: space saving wanted!
by Abigail-II (Bishop) on Apr 23, 2003 at 09:25 UTC
|
You could use symbolic references (which is not considered "neat")
or eval (which also isn't considered "neat"). Or you could
use the following untested code:
my @calcs = \my ($calc1, $calc2, $calc3, $calc4);
foreach my $calcref (@calcs) {
# Use $$calcref to access the $calc variables
}
But you are probably better off using an array instead of
a list of variables.
Abigail | [reply] [Watch: Dir/Any] [d/l] |
On sequentially-named variables
by merlyn (Sage) on Apr 23, 2003 at 13:51 UTC
|
Sequentially-named variables are almost always a sign of bad design.
Consider describing $calc3 by itself. What does the name mean? If in describing the variable in human terms, you mention "third" anywhere in the description, then the variable almost certainly should have been the third element of an array.
If it's the "third" of something, you're very likely to want to perform operations on the "first", "second", "third", and so on of that category (as you discovered). And the easiest way
to do that is with an array.
Just because Perl can frequently go behind the scenes and help you discover "all variables named as $calcnnn" doesn't mean that you should contemplate your navel in every single program. Too much introspection leads to fragile programs, and in that case, be very careful how much information you put about your authorship, because your maintenance programmer will hunt you down and kill you.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply. | [reply] [Watch: Dir/Any] |
Re: space saving wanted!
by smitz (Chaplain) on Apr 23, 2003 at 09:28 UTC
|
Have you considered using an array?
If thats really not possible, you could:
#!perl
$calc1 = 1;
$calc2 = 15;
$calc3 = 7.5;
$calc4 = 9;
for (1..4) {
my $var = 'calc' . $_;
print $var, "\t", $$var, "\n";
}
__END__
calc1 1
calc2 15
calc3 7.5
calc4 9
But that really is horrible. You should definately be using an array.
Smitz | [reply] [Watch: Dir/Any] [d/l] |
Re: space saving wanted!
by demerphq (Chancellor) on Apr 23, 2003 at 10:08 UTC
|
my ($calc1, $calc2, $calc3, $calc4);
foreach my $calc ( $calc1, $calc2, $calc3, $calc4 ) {
print "$calc\n";
}
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
How could i do this if i wanted to test each $count for being over 0 ??
e.g.
if ($calc < 0 ) {
print "$some_variable\n";
}
| [reply] [Watch: Dir/Any] [d/l] |
|
Well demerphq already gave you 99.9% of the code, just test to see if each variable is greater than zero:
my ($calc1, $calc2, $calc3, $calc4);
foreach my $calc ( $calc1, $calc2, $calc3, $calc4 ) {
print "$calc\n" if ($calc > 0);
}
--
vek
-- | [reply] [Watch: Dir/Any] [d/l] |
|
|
|
Why not get rid of that pesky loop?
my ($calc1, $calc2, $calc3, $calc4, $calc5, $calc6, $calc7);
print $calc1;
print $calc2;
print $calc3;
print $calc4;
print $calc5;
print $calc6;
print $calc7;
Smitz | [reply] [Watch: Dir/Any] [d/l] |
|
print join($/||"",$calc1, $calc2, $calc3, $calc4, $calc5, $calc6, $cal
+c7);
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
| [reply] [Watch: Dir/Any] [d/l] [select] |