How to construct new variables in a script from other variables and strings.

G

gerry.brennan

I wan to construct the following variable student is a string count is
a variable and name is a string
assign a value to it
print it

${"student_"$count"_name"} = "tom";

print ("$student_1_name\n");

Thank you.
 
J

Josef Moellers

I wan to construct the following variable student is a string count is
a variable and name is a string
assign a value to it
print it

${"student_"$count"_name"} = "tom";

print ("$student_1_name\n");

You want to use a hash.
 
U

usenet

${"student_"$count"_name"} = "tom";
print ("$student_1_name\n");

Well, you COULD do this:

#!/usr/bin/perl
my $count = 1;
${"student_${count}_name"} = "tom";
print ("$student_1_name\n");
__END__

But that's a VERY BAD idea (and, of course, it won't compile under "use
strict" which is a REQUIRED part of EVERY Perl program).

This is called a "symbolic reference" and doing this causes the earth
to explode. You cannot possibly imagine the utter destruction that
awaits you if you attempt to compile this program (or use this
technique). Please, for the sake of myself and my loved ones, do NOT
bring this destruction upon this fragile planet which we share.

Instead, rework your code to use a proper data structure. You don't
give enough context to form a recommendation, but it seems some sort of
hash is in order here.
 
M

Mirco Wahab

Hi gerry
I wan to construct the following variable student is a string count is
a variable and name is a string
assign a value to it
print it

${"student_"$count"_name"} = "tom";
print ("$student_1_name\n");

This is like when in the
olden times we used to beat
our wifes all day.

This is not done anymore.

So it these ${"string"}-things aren't
done in Perl anymore, as other
contributors already pointed out.

Today, Perl got arrays for
that:

$count = 1;
$students[$count] = "Tom"

$count = $count + 1;
$students[$count] = "Jerry"

foreach $name_of_student (@students) {
print $name_of_student, "\n";
}
# prints:
# Tom
# Jerry

Regards,

Mirco
 
U

usenet

Bernard El-Hagin wrote:
[df]> > "use strict" which is a REQUIRED part of EVERY Perl program).
s/REQUIRED/RECOMMENDED/;
s/EVERY/MOST/;

s/$Bernard_post//;

There is NEVER, absolutely NEVER a _GOOD_ reason why strict() should
not be part of each and every Perl program - NO exceptions EVER.

Bernard El-Hagin is a respected Perl coder and usenet contributor, but
I believe he is DEAD WRONG on this point. If he can justify this
hertodox opinion (with a cogent line of reasoning) then I invite him to
do so.

I will conceed that a script which is so trivial that it uses no named
variables will not necessairly profit from 'strict', but this is not
sufficient reason to abandon this 'best practice,' as 'strict' will not
impeed such a trivial script (but will accomodate scope creep).
 
T

Tad McClellan

Subject: How to construct new variables in a script from other variables and strings.


Your Question is Asked Frequently:

perldoc -q variable

How can I use a variable as a variable name?

I wan to construct the following variable


Hopefully, after reading the FAQ answer, your desire will become:

I wan to construct the following hash key
 
P

Paul Lalli

Bernard El-Hagin wrote:
[df]> > "use strict" which is a REQUIRED part of EVERY Perl program).
s/REQUIRED/RECOMMENDED/;
s/EVERY/MOST/;

s/$Bernard_post//;

There is NEVER, absolutely NEVER a _GOOD_ reason why strict() should
not be part of each and every Perl program - NO exceptions EVER.

This is simply blatantly false.

There are several times when strict should be disabled. Hell, even PBP
lists a few of them. You should go read it (page 433).

Then also take a look at the source code for modules such as Exporter
and explain how you would accomplish the same goals with strict 'refs'
enabled throughout the file.
Bernard El-Hagin is a respected Perl coder and usenet contributor

Yes, and you should listen to him.
but I believe he is DEAD WRONG on this point.

Your belief is inconsistent with fact.

Paul Lalli
 
T

Tad McClellan

Bernard El-Hagin wrote:
[df]> > "use strict" which is a REQUIRED part of EVERY Perl program).
s/REQUIRED/RECOMMENDED/;
s/EVERY/MOST/;

There is NEVER, absolutely NEVER a _GOOD_ reason why strict() should
not be part of each and every Perl program - NO exceptions EVER.
^^^^^^^^^^^^
^^^^^^^^^^^^

Some joker[1] once paraphrased me as saying:

Don't forget to enable warnings and strictures even when you're not
writing in Perl. It's *that* important.

Bernard El-Hagin is a respected Perl coder and usenet contributor


Ahhh, he's just the joker.



[1] Message-ID: <[email protected]>
 
B

Brian McCauley

Paul said:
Bernard El-Hagin wrote:
[df]> > "use strict" which is a REQUIRED part of EVERY Perl program).
s/REQUIRED/RECOMMENDED/;
s/EVERY/MOST/;

s/$Bernard_post//;

There is NEVER, absolutely NEVER a _GOOD_ reason why strict() should
not be part of each and every Perl program - NO exceptions EVER.

This is simply blatantly false.

There are several times when strict should be disabled.

Disabling strict (in a limited scope) is something completely
differrent from choosing not to enable strict in the outermost scope.
 
B

Brian McCauley

Bernard El-Hagin wrote:
[df]> > "use strict" which is a REQUIRED part of EVERY Perl program).
s/REQUIRED/RECOMMENDED/;
s/EVERY/MOST/;

s/$Bernard_post//;

There is NEVER, absolutely NEVER a _GOOD_ reason why strict() should
not be part of each and every Perl program - NO exceptions EVER.

I will conceed that a script which is so trivial that it uses no named
variables will not necessairly profit from 'strict', but this is not
sufficient reason to abandon this 'best practice,' as 'strict' will not
impeed such a trivial script (but will accomodate scope creep).

The rule of thumb I use is that once a script is big enough that I'm
putting in in a file rather than using -e then it's big enough to
beniefit from making it strict.

You'd be hard pressed to convince me it was worth using strict in -e
one-liners.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top