Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

"Globbing" over variable names in Perl?

by pat_mc (Pilgrim)
on Sep 29, 2010 at 17:01 UTC ( #862660=perlquestion: print w/replies, xml ) Need Help??
pat_mc has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks -

Here is my humble question for the day: Perl permits to access variables whose names were created in some (possibly systematic) way in the way illustrated in the following example.

perl -le '$a1 = 3; $a2 = 4; $c = 2; print ${a . $c}'
returns the correct output '4'.

So here's my question: How can I obtain the list of all variables whose name matches a certain pattern? I am thinking of something analogous to globbing ('$a*') over variable names. Ideally, I would like to write code like
for ( @a ) { print ${ $_ }; }
where @a should hold the names of all variables starting with $a, say, or mathing some other pattern of the variable name (not content!).

Did I explain this comprehensibly? And, if so, any help on this?

Cheers -


Replies are listed 'Best First'.
Re: "Globbing" over variable names in Perl?
by Anonymous Monk on Sep 29, 2010 at 17:16 UTC
    perl -le"print for keys %::" perl -le"print for keys %main::"
    $ perl -le"package stuff; $soy =1; $sauce =2; print for keys %stuff::" sauce soy
    $ perl -MData::Dumper -le"package stuff; $soy =1; $sauce =2; print &: +:Dumper( \%stuff:: ) " $VAR1 = { 'sauce' => *stuff::sauce, 'soy' => *stuff::soy };
      Thanks, this is cool stuff ... solution 2 is exactly what I was looking for! At least it returns a clean list of all variables defined. Could you briefly expand on the syntax, please? I am not familiar with package stuff.

      Thanks again -

        I am not familiar with package stuff.

        See package.

Re: "Globbing" over variable names in Perl?
by jethro (Monsignor) on Sep 29, 2010 at 17:11 UTC
    perl -le '$a1 = 3; $a2 = 4; $c = 2; print join " | ",(keys %main:: ); +'

    This works only for global variables, not the ones declared with 'my'

      Thanks, jethro... this is a good point to start from. However, print keys %main:: returns an awful lot of gibberish along with the desired variable names. Still, this array of hash keys would be a helpful starting point.

      Thanks again for your help!

Re: "Globbing" over variable names in Perl?
by DrHyde (Prior) on Sep 30, 2010 at 09:52 UTC
    Brother Jethro's reply answers the question as asked. However, if you want "dynamic" variable names, then you are very strongly advised to use a hash, so that you can calculate hash keys on the fly instead of variable names. This is far easier to make work with use strict, which will help avoid all kinds of subtle bugs, and just as importantly it will be easier for someone else to understand later. Hell, it'll be easier for you to understand later too.
Re: "Globbing" over variable names in Perl?
by suhailck (Friar) on Sep 29, 2010 at 17:29 UTC
    perl -e '$a1="1";$a2="2";$a4="3"; print map {q{$}.$_,"\t",${$_},"\n"} grep {m/^a.*$/}(sort keys %main:: +)' $a1 1 $a2 2 $a4 3

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://862660]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-05-20 23:58 GMT
Find Nodes?
    Voting Booth?