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:
perl variable name interpolation in a loop;

 



rmh_va
Novice

Aug 16, 2011, 7:04 PM

Post #1 of 17 (1733 views)
perl variable name interpolation in a loop; Can't Post

I have 8 passed variables from a dbi call...

$sub1, $sub2, $sub3...$sub8

in a loop, I want to use the interpolated name utilizing the loop value..

while ($i < 9) {

$temp = "$sub$i"; # or something like that... I can't make it work?

# do something with $temp...

}


FishMonger
Veteran / Moderator

Aug 16, 2011, 8:13 PM

Post #2 of 17 (1718 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post

You need to provide more details.

What are you trying to do with $temp?

Exactly what do you mean by "I can't make it work"?

Are you receiving any errors and/or warnings?

Do you have warnings enabled?


rmh_va
Novice

Aug 16, 2011, 8:27 PM

Post #3 of 17 (1715 views)
Re: [FishMonger] perl variable name interpolation in a loop; [In reply to] Can't Post

i can't get the interpolation to work in a loop... In my example, I have 8 variables with the names $sub1, $sub2, etc...

I tried to assign a temp vatiable to the interpolated result of the assign statement, utilizing the value of the loop...

the idea is to not have to hard code the variable names...

so I constructed a loop...

while ($i < 9) {

$temp = ("$sub"."$i");

@x = split(/\//, $temp); # data has embedded / between 3 fields;

print 'element 1 = ',$x[0],'';

print 'element 2 = ',$x[1],'';

print 'element 3 = ',$x[2],'';

}



I can't get the $temp variable to initialize... I have printed the array being returned by DBI, there is data in it... It's not the split, not the data.. it's the interpolation...

I tried printing $temp, it prints the value of the loop (1, 2, 3, etc.)...

there is something I'm not getting right in trying to interpolate the variable name in the loop...

Please, I don't need help with the split, the print, or anything else, only how to interpolate the variable name using the value of the loop...


FishMonger
Veteran / Moderator

Aug 16, 2011, 8:38 PM

Post #4 of 17 (1713 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post

What you're asking about is called symbolic references, which are bad and should not be used.

Most of the time when someone whats to use symbolic references, what they really want or should be using is a hash. In your case I'd use a foreach loop to loop over the 8 vars. Something like this:

Code
foreach my $var ( $sub1, $sub2, $sub3 ) { 
# do something with $var
}



rmh_va
Novice

Aug 16, 2011, 8:44 PM

Post #5 of 17 (1711 views)
Re: [FishMonger] perl variable name interpolation in a loop; [In reply to] Can't Post

I got it to work by referencing the array returned by dbi... I think i'll stick with that... Your way requires me to hard-code the variable names, which I was trying to avoid... I am familiar with interpolation a little bit, and I have successfully done it with this kind of var: $temp2 = ($FORM{"ou$i"."amt"});

this interpolates fine... I wanted to do the same thing here, so I wouldn't have to code the variable names...

there has to be a way to do this... I'll check some other boards...



thanks anyway...


rovf
Veteran

Aug 17, 2011, 2:58 AM

Post #6 of 17 (1704 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post


Quote
$temp = ("$sub"."$i");


I find it hard to understand what you mean (maybe you can provide a sample of data ... what variable you have, what values they have, what value the variables should have afterwards).

As for the piece of code above, do you maybe mean:


Code
$temp=eval('$sub'.$i)


?


rmh_va
Novice

Aug 17, 2011, 5:47 AM

Post #7 of 17 (1702 views)
Re: [rovf] perl variable name interpolation in a loop; [In reply to] Can't Post

I have 8 variables... I would like to reference them in a loop without hard-coding the names...

$sub1 = 'var1name/var1text';

$sub2 = 'var2name/var2text';

$sub3 = 'var3name/var3text';

$sub4 = 'var4name/var1text';

$sub5 = 'var5name/var1text';

$sub6 = 'var6name/var1text';

$sub7 = 'var7name/var7text';

$sub8 = 'var8name/var8text';

print '<table>';

while ($i < 9) {

$temp = "$sub.$i";

@xr = split("/",$temp);

print '<tr><td>',$xr[0],'</td><td>',$xr[1],'';

$i++;

}



have you ever done interpolation like this or similar? the "$temp = "$sub.$i";" statement is not working... I know this... I am trying to get the proper syntax to interpolate these variable names ($sub1, $sub2, etc.) in the loop so that I do not have to hard code them...


rovf
Veteran

Aug 17, 2011, 6:01 AM

Post #8 of 17 (1699 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post

And what is wrong with the solution I gave you?


rmh_va
Novice

Aug 17, 2011, 6:06 AM

Post #9 of 17 (1697 views)
Re: [rovf] perl variable name interpolation in a loop; [In reply to] Can't Post

it doesn't work... I can print the table with data in it but when I use your statement, the array is empty... sorry, this doesn't work...

you should be able to replicate this with the code snippet I provided... you don't appear to be understanding what it is I want to do... Let me reiterate again...



I want to be able to reference literals in a loop, by interpolation, using the value fo the loop index...



my question, again, is do you know how to interpolate variable names?


FishMonger
Veteran / Moderator

Aug 17, 2011, 6:18 AM

Post #10 of 17 (1695 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post

Why do you want to make it more difficult than it needs to be and at the same time use a very bad method?

Put the vars in an array or hash and simply loop over that data. If you need help with that, you'll need to post your current db code that creates/assigns these 8 vars so that we can show you how to do it properly.


FishMonger
Veteran / Moderator

Aug 17, 2011, 6:22 AM

Post #11 of 17 (1694 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post


Quote
I want to be able to reference literals in a loop, by interpolation, using the value fo the loop index...

That's accomplished by using an array instead of 8 separate vars.


Quote
my question, again, is do you know how to interpolate variable names?

That's accomplished by using a hash instead of the 8 separate vars.

The approach you're attempting to use is called symbolic references, which is a very bad idea.


rmh_va
Novice

Aug 17, 2011, 6:43 AM

Post #12 of 17 (1691 views)
Re: [FishMonger] perl variable name interpolation in a loop; [In reply to] Can't Post

Understood, you will not answer my specific question as to whether you know how to interpolate variables or not... I guess you do not know how to do this... Your attempts at "resolving" my question have been a huge waste of my time... If you are not knowledgeable in a specific area, you should let someone know, instead of posturing, and faking it...


rovf
Veteran

Aug 17, 2011, 6:58 AM

Post #13 of 17 (1687 views)
Re: [FishMonger] perl variable name interpolation in a loop; [In reply to] Can't Post

Some people are funny, aren't they? ;-)


FishMonger
Veteran / Moderator

Aug 17, 2011, 7:26 AM

Post #14 of 17 (1684 views)
Re: [rmh_va] perl variable name interpolation in a loop; [In reply to] Can't Post

So, you're saying that you intentionally want to write bad code instead of taking the advise of others that have far more Perl knowledge than yourself?

http://www.perl.com/doc/FMTEYEWTK/style/slide24.html


rmh_va
Novice

Aug 17, 2011, 7:32 AM

Post #15 of 17 (1679 views)
Re: [FishMonger] perl variable name interpolation in a loop; [In reply to] Can't Post

No I'm HERE because I want to get advice... what I DON'T want is someone who will not answer a specific question trying to tell me what I need to do... You will not respond to my very specific question, which leads me to believe you are not knowledgeable in this area, and therefore shouldn't be commenting or offering suggestions...

I am in a situaiton where I am passed data and unable to change the method I'm passed data to... I asked you a very simple, specific question about variable name interpolation, and you've danced all around it without once answering the question....



thanks for the run-around....


sooflife
New User

Aug 18, 2011, 11:36 PM

Post #16 of 17 (1660 views)
Re: [FishMonger] perl variable name interpolation in a loop; [In reply to] Can't Post

HI, I'm a beginner. I have try it as the website said but it not work too , and I think if it is better like below.

while ($i < 9) {

$temp = ${'sub'.$i}; # instead of $temp="$sub.$i";

@xr = split("/",$temp);

print '<tr><td>',$xr[0],'</td><td>',$xr[1],''."\n";

$i++;

}

it can works well on perl 5.010+;


BillKSmith
Veteran

Aug 19, 2011, 8:15 AM

Post #17 of 17 (1647 views)
Re: [sooflife] perl variable name interpolation in a loop; [In reply to] Can't Post

Refer to the section "Symbolic References" in perldoc perlref for an explanation of why this only "works" with global symbols and then only when "no strict refs" is in effect, and also for an explanation of why this is usually a poor practice.

The risk can be reduced by following the usual advice to use strict and use lexical (my) variables. Declare variables global (with "our") only when necessary. Relax the stricture on refs in the smallest possible scope.

Readonly is not necessary, but prevents accidental changes to to scalars which are clearly intended to be constants. This risk is significantly higher than usual in a program that uses symbolic references.

I have changed the format of the output only to emphasize the success of the symbolic reference.


Code
use strict; 
use warnings;
use Readonly;
Readonly::Scalar our $sub1 => 'var1name/var1text';
Readonly::Scalar our $sub2 => 'var2name/var2text';
Readonly::Scalar our $sub3 => 'var3name/var3text';
Readonly::Scalar our $sub4 => 'var4name/var1text';
Readonly::Scalar our $sub5 => 'var5name/var1text';
Readonly::Scalar our $sub6 => 'var6name/var1text';
Readonly::Scalar our $sub7 => 'var7name/var7text';
Readonly::Scalar our $sub8 => 'var8name/var8text';
print '<table>', "\n";
foreach my $i (1..8) {
my $temp = do {no strict 'refs'; ${'sub' . $i};};
my ($name, $text) = split( "/", $temp );
print '<tr><td>', $name, '</td><td>', $text, "\n";
}
print '</table>', "\n";

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