Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > assign a left hand variable to a variable

Reply
Thread Tools

assign a left hand variable to a variable

 
 
justme
Guest
Posts: n/a
 
      09-30-2004
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.
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      09-30-2004
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 <(E-Mail Removed)>
"Why shouldn't truth be stranger than fiction? Fiction, after all, has
to make sense." (Mark Twain)
 
Reply With Quote
 
 
 
 
Graham Wood
Guest
Posts: n/a
 
      09-30-2004
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-30-2004
justme <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
left shift operator behaves like left rotate when the operand is a variable. pc C Programming 2 06-08-2011 06:58 PM
Microsoft Left Hand, Meet Microsoft Right Hand Lawrence D'Oliveiro NZ Computing 1 03-07-2011 03:55 AM
Seek cooperation Expect hand in hand xianzeguanggao Digital Photography 0 06-14-2007 08:07 AM
Left panel on left hand side of desktop on windows xp wish to get rid of Bun Mui Computer Support 1 09-14-2004 03:40 AM
Left-hand menu in CSS Kim André Akerĝ HTML 7 06-07-2004 06:58 PM



Advertisments