CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Count and Variables.

 



perlmonger2020
New User

Feb 8, 2016, 6:59 PM

Post #1 of 2 (1087 views)
Count and Variables. Can't Post

Hello,

Is there a simpler way to write this using some other method such as hash or something? Above this code there's more code that parses a line and has the value for $v1, $v2 ... $v15.


Code
for(my $i=1;$i<=15;$i++){ 
if($i==1){
$val = $v1;
}elsif($i==2){
$val = $v2;
}elsif($i==3){
$val = $v3;
}elsif($i==4){
$val = $v4;
}elsif($i==5){
$val = $v5;
}elsif($i==6){
$val = $v6;
}elsif($i==7){
$val = $v7;
}elsif($i==8){
$val = $v8;
}elsif($i==9){
$val = $v9;
}elsif($i==10){
$val = $v10;
}elsif($i==11){
$val = $v11;
}elsif($i==12){
$val = $v12;
}elsif($i==13){
$val = $v13;
}elsif($i==14){
$val = $v14;
}elsif($i==15){
$val = $v15;
}
}



BillKSmith
Veteran

Feb 9, 2016, 5:52 AM

Post #2 of 2 (1066 views)
Re: [perlmonger2020] Count and Variables. [In reply to] Can't Post

You need an array, not a hash.

Code
my @vals = (undef, $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9, 
$v10, $v11, $v12, $v13, $v14, $v15);
for my $i (1..15) {
my $val = $vals[$i];
}


It would be better to rewrite your parser to store its results in the array rather than using the fifteen scalar variables.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives