assign a left hand variable to a variable

J

justme

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.
 
T

Tore Aursand

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[$_] = ....;
}
 
G

Graham Wood

justme said:
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
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top