The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-27-2009
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Code:
# No "my" here
$a1 = "A" ;
$a2 = "B" ;
$a3 = "C" ;

foreach my $k ( 1,2,3 )
{
    my $msg = ${"a${k}"};
    print "$msg\n" ;
}
This is symbolic reference, and is not typically recommend. It works for variables in the symbol table only, so that means you cannot use this trick for variables that are lexically scoped (i.e. "my").

Are you really sure you want this? I can't think of any reason to advocate such constructs in typical programs except specialized modules that need to mess with the symbol table directly.