Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

space saving wanted!

by Anonymous Monk
on Apr 23, 2003 at 09:21 UTC ( #252487=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I wondered if there was a neat way of foreaching through a list of varibles that are names sequentially. If there is this would save me lots of typing. cheers x e.g.
my ($calc1, $calc2, $calc3, $calc4); foreach ( #ITERATE THROUGH EACH CALC VARIABLE ) { print "WHATEVER......"; }

Replies are listed 'Best First'.
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.


      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.

      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

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.


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.

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.

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"; }


    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
      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"; }
        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 --
      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;

        Well, lets see. The OP asked for a way to use a loop to reduce his typing. He also is unlikely to want to _only_ print the variables involved. That could lead to serious code duplication. Also if you are being facetious then why not get rid of all those pesky print statements?

        print join($/||"",$calc1, $calc2, $calc3, $calc4, $calc5, $calc6, $cal +c7);


        <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252487]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-09-25 14:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found