From: Mike Stok Date: 2006-09-11T03:45:04+09:00 Subject: Re: patching strings together to make a variable On 10-Sep-06, at 2:31 PM, dblack@wobblini.net wrote: > Hi -- > > On Mon, 11 Sep 2006, Axis Sivitz wrote: > >> Uh...if you have so many similarly named variables, it sounds like >> you >> should be using an array or a map. >> >> The map could be set up with the keys (a certain string) indexing to >> the values of your old variables. >> >> @some_map['var'] = "pass" >> >> def test(string) >> puts @some_map[string] >> end >> >> test('var') #=> "pass" > > Which brings us back to: > >> http://www.perl.org :-) Perl has these "soft" or "symbolic" >> references, plus a whole chorus of people to tell you that you >> should use hashes instead :-) > > :-) One of the reasons for the Perl chorus is that in Perl the "soft references" can only point to non-lexical variables, and this can cause much opportunity for enlightenment later! #!/usr/bin/env perl # use strict; # use warnings; $foo = 'foo1'; my $foo = 'foo2'; $soft_ref = 'foo'; $real_ref = \$foo; print "soft says $$soft_ref\n"; print "real says $$real_ref\n"; __END__ produces: soft says foo1 real says foo2 ... which is one of the reasons the strict and warnings pragmata are considered useful. Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.