Declare and Assign Variables In Short Way

T

TS

Hi ,

If I want to declare many variables $X1, $X2, $X3...$X20 and assign
all these variables with same default value, 10, how could I do in
short way instead of typing long nasty code like below?

$X1=10;
$X2=10;
$X3=10;
|
|
|
$X20=10;

Thanks
 
J

Josef Moellers

TS said:
Hi ,

If I want to declare many variables $X1, $X2, $X3...$X20 and assign
all these variables with same default value, 10, how could I do in
short way instead of typing long nasty code like below?

$X1=10;
$X2=10;
$X3=10;
|
|
|
$X20=10;

Thanks

Better: use an array:
@X = (10) x 21;

Your variables are then $X[0] through $X[20], you'd lose one variable if
the naming scheme should be kept.
 
G

Gunnar Hjalmarsson

TS said:
If I want to declare many variables $X1, $X2, $X3...$X20 and assign
all these variables with same default value, 10, how could I do in
short way instead of typing long nasty code like below?

$X1=10;
$X2=10;
$X3=10;
|
|
|
$X20=10;

For e.g. three variables you can do:

my ($X1,$X2,$X3) = (10) x 3;

But are you sure you don't want an array

my @X = (10) x 20;

or a hash

my %X = map { $_ => 10 } 1..20;

instead?
 
S

Sherm Pendley

TS said:

Thanks to whom? For what? Please quote enough of the message you're replying
to for your own message to make sense to everyone, not just those who happen
to be using Google to view entire threads at once.

sherm--
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top