Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Declare and Assign Variables In Short Way

Reply
Thread Tools

Declare and Assign Variables In Short Way

 
 
TS
Guest
Posts: n/a
 
      06-27-2005
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

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      06-27-2005
TS wrote:
> 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.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2005
TS wrote:
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      06-27-2005
"TS" <(E-Mail Removed)> writes:

> Thanks.


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--
 
Reply With Quote
 
TS
Guest
Posts: n/a
 
      06-27-2005
Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
easiest way to split a list into evenly divisble smaller lists, and assign them to variables? flamesrock Python 3 06-07-2005 02:17 AM
Best Way to Define/Declare/Initialize Variables Simultaneously? David T. Ashley C Programming 6 12-05-2004 09:37 AM
Best Way to Define/Declare/Initialize Variables Simultaneously? David T. Ashley C++ 6 12-05-2004 09:37 AM
newbiw question: What is the best way to declare and define global variables? david C Programming 1 10-06-2004 04:04 PM
What's the best way to declare a lot of page level variables when your page is broken up into user controls? Brent ASP .Net Web Controls 0 12-26-2003 12:47 AM



Advertisments