Gossamer Forum
Home : General : Perl Programming :

$ value within another

Quote Reply
$ value within another
I've defined a series of variables, including $s101, $s102, $s103 etc.

Can I use a "for my $i" statement to run out the 101, 102, 103 series in such a way as to print out the values of $s101, $s102 etc.?

On a previous occasion, I've worked on taking a literal expression, such as "record name" and linking it with a statement such as:

"recordname".$i

which worked fine for that purpose.

For my present need, various attempts around $s.$i or similar simply produce the result of:

.$i

because, of course, $s is not defined.

------------------
WYSIWYG

Regn # 1718-00-DB

[This message has been edited by WYSIWYG (edited February 12, 2000).]
Quote Reply
Re: $ value within another In reply to
Not sure I understand.
print "whatever you want $s101 $s102 $s103";
Quote Reply
Re: $ value within another In reply to
Sorry for not being clear.

I've defined over 100 values as follows:
$s101 = "Fred";
$s102 = "Henrietta";
$s103 = "Jane";
etc.

and I want to call these in sequence in various places in my script. I'm trying to run out the values using:

for (my $i =101; $i <= 215; $i++) {
print qq| $s.$i |;
}

(In fact, the statement is considerably longer than this.)

So, I'm trying to get this to run out script with several occurrences of $s101 -- then repeating the same for $s102 and so on.

But I can't work out how to get the "for my $i" routine to give a result of $s101 (the $s.$i above just gives [blank] .101 as $s has no value).

------------------
WYSIWYG

Regn # 1718-00-DB
Quote Reply
Re: $ value within another In reply to
for (my $i =101; $i <= 215; $i++) {
$to_eval = "print \"\$s$i\"";
eval($to_eval);
}



------------------
Chris
Quote Reply
Re: $ value within another In reply to
Thanks!