Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Adding scalars but with a twist..

by davido (Cardinal)
on Feb 14, 2005 at 03:15 UTC ( #430671=note: print w/replies, xml ) Need Help??


in reply to Adding scalars but with a twist..

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://430671]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-05-28 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?