Declare and Assign Variables In Short Way

Discussion in 'Perl Misc' started by TS, Jun 27, 2005.

  1. TS

    TS Guest

    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
     
    TS, Jun 27, 2005
    #1
    1. Advertising

  2. 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
     
    Josef Moellers, Jun 27, 2005
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Jun 27, 2005
    #3
  4. Re: Declare and Assign Variables In Short Way

    "TS" <> 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--
     
    Sherm Pendley, Jun 27, 2005
    #4
  5. TS

    TS Guest

    Re: Declare and Assign Variables In Short Way

    Thanks.
     
    TS, Jun 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tee
    Replies:
    1
    Views:
    322
    mikeb
    Jun 7, 2004
  2. David T. Ashley
    Replies:
    6
    Views:
    588
    Tim Rentsch
    Dec 5, 2004
  3. flamesrock
    Replies:
    3
    Views:
    650
    gene tani
    Jun 7, 2005
  4. David T. Ashley
    Replies:
    6
    Views:
    405
    Tim Rentsch
    Dec 5, 2004
  5. Andre
    Replies:
    5
    Views:
    564
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page