perl get variable value ???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl get variable value ???
# 1  
Old 05-27-2009
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  
Old 05-27-2009
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  
Old 05-27-2009
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  
Old 05-27-2009
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  
Old 05-28-2009
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL $0 variable

In PERL , $0 variable displays program name ( if we use inside script) .likewise is there a way to display program name and it's arguments passed to script . e.g. test.pl -a1 -b3 -c4 inside test.pl , if I use $0 , it gives me test.pl ..but I am looking for command to get complete program... (1 Reply)
Discussion started by: talashil
1 Replies

2. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

3. Shell Programming and Scripting

how to declare variable in perl

how can i declare variable in perl. for BLOCK in /sys/block/emcpow* (3 Replies)
Discussion started by: learnbash
3 Replies

4. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

5. Shell Programming and Scripting

PERL : Use of a variable in a tr

Hi, I want to count the number of occurences of a character in a string variable ($str). The character is stored in a another variable ($sepchr). I am using tr as : $count = ($str =~ tr/$sepchr//); This did not work. I found in another thread about using eval. I used eval as : ... (13 Replies)
Discussion started by: sinpeak
13 Replies

6. Shell Programming and Scripting

Perl variable declaration

what is the meaning of this particular line of code in perl. my %global_port2lanid = (); (2 Replies)
Discussion started by: suvenduperl
2 Replies

7. Shell Programming and Scripting

perl - how can we name a variable base on value of another variable

Hey all, perl - how can we name a variable base on the value of another variable? for example in ksh/bash we do : export c="100" export x`echo $c`=2000 echo $x100 x100=2000 is it possible to do something similar for perl? I already tried many ways but nothing is working. I am... (3 Replies)
Discussion started by: cacm1975
3 Replies

8. Shell Programming and Scripting

Multiple variable in a variable in Perl

Hi All, I am trying to convert the below Csh while loop into Perl while loop but the problem is that in this csh script, i have 2 variables inside a variable -> $count is a variable {SB$count} as a whole is another variable. Csh is able to assign values to such variable like the below but i do... (3 Replies)
Discussion started by: Raynon
3 Replies

9. Shell Programming and Scripting

perl not reading my variable

I'm trying to make changes in a file using the following bash script: #!/bin/bash MYHOME=`echo $HOME` README=$MYHOME"/environment" IAM=`whoami` CHANGEPATHLIST="TALOG TACONFIG TAINFO TAWORK TMPSPACE" for var in $CHANGEPATHLIST do perl -pi -e 's/sacuser1/$IAM/ if m/$var/' $README... (3 Replies)
Discussion started by: yoonixq4u
3 Replies

10. Shell Programming and Scripting

perl - variable inheritance

Hey Everyone, Does anyone know how - or if it's even possible - for a child perl script to inherit the variables of a parent perl script? In a shell script, you would use "export" for example. I am running Perl 5.8. Basically, let's say "perl1.pl" calls "perl2.pl" and I want "perl2.pl" to... (2 Replies)
Discussion started by: gsatch
2 Replies
Login or Register to Ask a Question