Multiple variable in a variable in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple variable in a variable in Perl
# 1  
Old 05-04-2009
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 not know if this is possible in Perl.
Can any expert give some advice?


[code]
while ( $count <= 5 ) then
set SB$count = `cat $files_extracted.txt|grep -c "x= $count"`
@ count = $count + 1
end
[code]
# 2  
Old 05-04-2009
This isn't a "variable inside a variable" (which is possible in Perl using references for example), but a bad excuse for an array. A literal translation of your code would be something like
Code:
for($i=0;$i<=5;$i++) {
    $SB[$count] = `grep -c "x=$count" $files_extracted.txt`;
}

Even shaved off a line and one unnecessary cat. Be prepared to do away with a lot of CSH workarounds now that you use a real scripting language.
# 3  
Old 05-04-2009
Hi Pludi,

Thks for the advice.
But if my csh statement is the below. How can i convert the below to Perl ?

Code:
set SB$count = `cat $tester_dir/files_extracted.txt|grep "HARD_BIN:"|awk '{print ($2)}'|grep -c $count`

# 4  
Old 05-04-2009
Code:
$SB1 = `cat $tester_dir/files_extracted.txt|grep "HARD_BIN:"|awk '{print \$2}'|grep -c $count`;

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl multiple qr assigned to variable

Experts, I'm having problems with the code below. I'm trying to test $var2 for two different regexs. I thought it could be done per below, but I'm getting the following error when running. $ ./test.pl b fed50c0100**** Unescaped left brace in regex is deprecated, passed through in regex; marked... (2 Replies)
Discussion started by: timj123
2 Replies

2. Shell Programming and Scripting

How to create a variable without multiple concats in a perl scripts that makes a bsub?

The red text at the bottom represents the three lines I want to address. I'm dynamically creating a bsub with a perl script and would like to create the fasta_16S variable in a single line....not three. I'm having difficulty in getting the syntax correct. Obviously, there is some confusion... (3 Replies)
Discussion started by: jdilts
3 Replies

3. 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

4. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

5. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

6. 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

7. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

8. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

9. Shell Programming and Scripting

Define multiple mail recipents in a variable in perl

hi I have a perl script from which I call a shell script and pass mail variable to it. The mail works fine if I give 1 recipient but fails for multiple. conv.pl:- $mialing = "anu\@abc.com" rest.sh $mialing rest.sh mail -s "hi" $1 This works fine But I need to define multiple... (2 Replies)
Discussion started by: infyanurag
2 Replies

10. 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
Login or Register to Ask a Question