enlarge Variablenames

U

Ugur Boss

my Problem ist following:

$nr1 = 1;
$nr2 = 2;

$name.$nr = "Hello";

I have tried following way:

${"name".$nr} = "Hello";

But it doesn´t work. Is there another way to solve my problem?

greetings
 
P

Paul Lalli

my Problem ist following:

$nr1 = 1;
$nr2 = 2;

$name.$nr = "Hello";

I have tried following way:

${"name".$nr} = "Hello";

But it doesn´t work. Is there another way to solve my problem?

You haven't described what your problem is. What exactly are you trying
to achieve?

Note that I'm going to make a *guess* that you are trying to use a
variable as a variable name. This is A Bad Idea. For more information on
why this is A Bad Idea, and how to do what you *should* do instead, please
read the FAQ entry "How can I use a variable as a variable name":

perldoc -q 'variable name'

Paul Lalli
 
J

Jürgen Exner

Ugur said:
my Problem ist following:

$nr1 = 1;
$nr2 = 2;

$name.$nr = "Hello";

This is not Perl. Are you thinking about a "record" like in Modula or
Pascal?
The closest Perl data structure would be a hash:
$name{$nr} = "Hello";

jue
 
U

Ugur Boss

I try to enlarge the variablename of my scalar $name so that i have
later a variable named $name666 or $name999 with the value "Hello".
 
P

Paul Lalli

[please post your replies *below* the text you are replying to]

I try to enlarge the variablename of my scalar $name so that i have
later a variable named $name666 or $name999 with the value "Hello".

Yes. This is what I said. You are trying to use a variable (in this case
$nr1 or $nr2) as part of a variable name. This is not a good idea.
Please read the FAQ I mentioned in my previous reply. If you do not
understand something in that FAQ, let us know what you don't understand.

Paul Lalli
 
J

Jürgen Exner

[Please do not top-post]
[Please trim the quoted text to a reasonble amout that is needed for
context]
Ugur said:
I try to enlarge the variablename of my scalar $name so that i have
later a variable named $name666 or $name999 with the value "Hello".

This is A Bad Idea.
Please see 'perldoc -q "variable name"' why and what to do instead.

jue
 
A

Andrew DeFaria

Jürgen Exner said:
[Please do not top-post]
[Please trim the quoted text to a reasonble amout that is needed for
context]

Also, aren't these top posts? ;-)

Not I whole heartedly agree with Jügen, trim, trim, trim! Way too much
excess is quoted.
 
G

gnari

Andrew DeFaria said:
Jürgen Exner said:
[Please do not top-post]
[Please trim the quoted text to a reasonble amout that is needed for
context]

Also, aren't these top posts? ;-)

not really. these were general comments that did not depend on any
quoted text. Jürgen could have stopped there but decided to
also reply to the top-poster, and did that properly quoted, of
course

gnari
 
J

Joe Smith

Ugur said:
my Problem ist following:

$nr1 = 1;
$nr2 = 2;

$name.$nr = "Hello";

my %name; # Use a hash
$name{666} = "Number of the beast";
$name{foo} = "bar";
$name{$nr1} = "one";
$name{$nr2} = "two";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top