assign a left hand variable to a variable

Discussion in 'Perl Misc' started by justme, Sep 30, 2004.

  1. justme

    justme Guest

    hi

    how do i assign a left hand side variable to a variable. For example


    for($count = 0; $count < @array; $count++ )
    {
    $h = $somevariable->somemethod(args,args);
    }

    i want to have something like this:

    $h0 = $somevariable->somemethod(args,args);
    $h1 = $somevariable->somemethod(args,args);
    $h3 = $somevariable->somemethod(args,args);
    ......

    How to "append" the numbers to $h ? thanks in advance.
     
    justme, Sep 30, 2004
    #1
    1. Advertising

  2. justme

    Tore Aursand Guest

    On Thu, 30 Sep 2004 00:44:15 -0700, justme wrote:
    > how do i assign a left hand side variable to a variable. For example
    >
    >
    > for($count = 0; $count < @array; $count++ )
    > {
    > $h = $somevariable->somemethod(args,args);
    > }
    >
    > i want to have something like this:
    >
    > $h0 = $somevariable->somemethod(args,args);
    > $h1 = $somevariable->somemethod(args,args);
    > $h3 = $somevariable->somemethod(args,args);
    > .....
    >
    > How to "append" the numbers to $h ? thanks in advance.


    You most probably won't do that [1], and you're anyway better off using an
    array, as far as I can see;

    for ( 0..$#array ) {
    $h[$_] = ....;
    }


    --
    Tore Aursand <>
    "Why shouldn't truth be stranger than fiction? Fiction, after all, has
    to make sense." (Mark Twain)
     
    Tore Aursand, Sep 30, 2004
    #2
    1. Advertising

  3. justme

    Graham Wood Guest

    justme wrote:
    >
    > hi
    >
    > how do i assign a left hand side variable to a variable. For example
    >
    > for($count = 0; $count < @array; $count++ )
    > {
    > $h = $somevariable->somemethod(args,args);
    > }
    >
    > i want to have something like this:
    >
    > $h0 = $somevariable->somemethod(args,args);
    > $h1 = $somevariable->somemethod(args,args);
    > $h3 = $somevariable->somemethod(args,args);
    > .....
    >
    > How to "append" the numbers to $h ? thanks in advance.


    Don't. Use an array if you just want it indexed with numbers or a hash
    if you prefer a string index.

    for($count = 0; $count < @array; $count++ )
    {
    $h[$count] = $somevariable->somemethod(args,args);
    }

    You could write something like ...
    $varname = "h$count";
    $$varname = $somevariable->somemethod(args,args);

    but that is what's known as a symbolic reference and is generally
    frowned upon unless you know what you're doing and have a good reason
    for not using a hash or an array. Symoblic references can lead to the
    dreaded "unexpected behaviour".

    Graham
     
    Graham Wood, Sep 30, 2004
    #3
  4. justme <> wrote:

    > how do i assign a left hand side variable to a variable.

    ^^^^^^^^

    perldoc -q variable

    How can I use a variable as a variable name?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 30, 2004
    #4
    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. Kim André Akerø

    Left-hand menu in CSS

    Kim André Akerø, Jun 4, 2004, in forum: HTML
    Replies:
    7
    Views:
    3,200
    Beauregard T. Shagnasty
    Jun 7, 2004
  2. Bosconian
    Replies:
    1
    Views:
    1,393
    Bosconian
    May 9, 2006
  3. It's me
    Replies:
    45
    Views:
    1,144
    Peter Hansen
    Dec 10, 2004
  4. =?Utf-8?B?YnJ1bm8=?=

    Left and Right hand splitting and resizing

    =?Utf-8?B?YnJ1bm8=?=, Sep 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    4,164
    Walter Wang [MSFT]
    Sep 21, 2006
  5. Simon

    Outlook style left hand menu bar

    Simon, May 30, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    699
    bruce barker
    May 30, 2007
Loading...

Share This Page