The UNIX and Linux Forums  


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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multiple variable in a variable in Perl Raynon Shell Programming and Scripting 3 05-04-2009 11:10 PM
Pass csh variable to Perl Raynon Shell Programming and Scripting 9 10-19-2007 10:46 PM
perl not reading my variable yoonixq4u Shell Programming and Scripting 3 08-24-2007 10:24 PM
PERL: how to tell if variable is NULL dangral Shell Programming and Scripting 4 03-19-2003 01:34 PM
perl variable assingment seismic_willy Shell Programming and Scripting 2 01-29-2002 04:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 139
perl get variable value ???

hi i have following code

Code:
my $a1 = "A" ;
my $a2 = "B" ;
my $a3 = "C" ;

foreach my $k ( 1,2,3 ) 
{
    my $msg = ${a{$k}} # this should be at runtime i am creating variable a1 and assigning it value to msg .
    print "$msg\n" ;
}

above thing is not working !!!
i want when k = 1 msg = "A"

how can i do that ??

-----Post Update-----

i got it working i created array instead of 3 variables .. but still if you can tell me how can do it without array just for curiosity ...

Last edited by zedex; 05-27-2009 at 07:41 AM.. Reason: removed extra update ...
  #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.
  #3 (permalink)  
Old 05-27-2009
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 139
I got it working with array.

actually there was a part in code which was repeating. like checking 5,6,7 argument lenght is 1 and then its valid entry so after checking i wanted to print which argument was wrong.


Code:
$ARG5 = "ADD ENTRY TO DATABASE" ;
$ARG6 = "ADD ENTRY TO REG FILE" ;
$ARG7 = "SHOW DEBUG MSG" ;

foreach $k in ( 5,6,7) 
{ 
     if ...
     {
      } else 
      { 
             print ${"ARG$k"} entry invalid\n" ;      
       }
}
 
so i created array 

@ARG = ( undef ,undef, ..., "ADD ENTRY TO REG FILE",...) ;

  #4 (permalink)  
Old 05-27-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
zedex,

what you want to do is use a hash.

Assuming the rest of your code works:


Code:
my %ARG = (
   5 => "ADD ENTRY TO DATABASE",
   6 => "ADD ENTRY TO REG FILE",
   7 => "SHOW DEBUG MSG",
);

foreach $k in (5,6,7) {
{ 
     if ...
     {
      } else 
      { 
             print "$ARG{$k} entry invalid\n" ;      
       }
}

Hashes are a fundamental concept of perl (and other programming languages), you need to read up on them and use them as necessary.
  #5 (permalink)  
Old 05-28-2009
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 139
thanks KevinADC

actually i was involved in many things so forgot about simple use of HASH. even though i used hash for this same reason previously.. any ways nice to know how to do it
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0