Remember how in your previous thread I mentioned that the global symbol table is, itself, a hash? So here's the strategy: grep the global symbol table's keys for names that match the pattern " /Sel\d+Ttl/". That's all the variables you're looking for. Next, iterate through those 'keys', dereferencing the global symbol table to get at the values held in those key names (variable names). Here's an example in action. Remember, I warned you previously this is a road to perdition. I'll see you in hell. ;) (just teasing)

no strict qw/vars/; ( $sel1, $sel2, $sel3, $sel4, $sel5 ) = ( 1, 2, 3, 4, 5 ); my $sum = 0; $sum += ${$::{$_}} foreach grep /sel\d+/, keys %::; print $sum, "\n";

Magic!

Update: I realize that you could just dereference the individual keynames, as in  $$_, within the foreach loop, but I left the use of the global symbol hash in the code so that future maintainers would know without a doubt that there is something going on here that needs further investigation to understand. $$_ isn't obvious enough to cry out, "I'm a symbolic ref! Beware!"


Dave


In reply to Re: Adding scalars but with a twist.. by davido
in thread Adding scalars but with a twist.. by snappybo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.