Solved

# Fatal error: Unsupported operand types

Posted on 2005-04-27
Medium Priority
8,931 Views
I'm trying to convert a Perl function to PHP, I get a Fatal error: Unsupported operand types on line that starts with \$rank.

I think that is because \$total is actually an array but not sure. I could use some help. I asked a question on how to convert entire function. But I thought a more concise question may be easier to get help on.

Thanks!!

for (\$i=0;\$i<6;\$i++) {

for (\$judge = 0; \$judge < 5; \$judge++) {
\$place=\$placements[\$judge][\$i];
\$total[\$place]++;
\$sum[\$place] += \$place;
}

for (\$place=0;\$place<6;\$place++) {
\$sum += \$sum[\$place];
\$rank[\$couple]=array(\$place,-\$total,\$sum-\$total[++\$place]);
if ((\$total += \$total[\$place]) > 3) {
break;
}
}

}

Function in perl is
for my \$place( 1..@couples ){
\$sum += \$sum[\$place];
\$rank[\$couple]=[\$place,-\$total,\$sum,-\$total[++\$place]], last if (\$total += \$total[\$place]) > @placements/2;
}
0
Question by:BrentNJ
• 7
• 2
• 2

Author Comment

ID: 13877217
Should have read \$rank[\$i] but same problem
0

LVL 9

Assisted Solution

AlanJDM earned 880 total points
ID: 13879068
array(\$place,-\$total,\$sum-\$total[++\$place]) <----- problem

array() function doesn't accept parameters like that. The only parameters you can pass to the array() function is index/value pairs...

array(inde1 => value1, index1 => value2)

http://us3.php.net/manual/en/function.array.php

Alan
0

LVL 9

Expert Comment

ID: 13879091
typo...

array(inde1 => value1, index1 => value2)

should be...

array(inde1 => value1, index2 => value2)

Sorry. Thats like the 5th typo Ive made today... I think it is time to take a break.

Alan

0

LVL 6

Accepted Solution

German_Rumm earned 1120 total points
ID: 13879546
Hi BrentNJ,

You problem is on the line
\$sum += \$sum[\$place];

You can't add to array this way. If you want to consecutively add all \$sum elements, use some other variable name, like
\$sum2:

\$sum2 += \$sum[\$place];
\$rank[\$couple] = array(\$place, -\$total, \$sum2 - \$total[++\$place]);

Also, there is some possible problem: you increment \$place inside loop, which is not what you want probably... Use \$place + 1 instead.

AlanJDM,
you are not correct, defining array like array('element1', 'element2', 'element3') is perfectly valid.

Hope this helps!
0

Author Comment

ID: 13879885
Thanks!! I'll give it a try now.
0

Author Comment

ID: 13879937
I actually had the sum2 in one of my tries - same problem.

Is it OK to reference the array \$total as just -\$total?

Maybe perl treats \$total differently.
0

LVL 6

Expert Comment

ID: 13880058
BrentNJ,

oops, didn't notice it. no, you cannot reference array this way.
If you want to get sum of all \$total elements you should use array_sum(\$total), in example

\$rank[\$i] = array(\$place, -array_sum(\$total), \$sum2 - \$total[\$place+1]);

I'm sorry if this is not what you want, I just don't understand what your script does with these numbers :-)
0

Author Comment

ID: 13880861
This is what I did so far

for (\$place=0;\$place<6;\$place++) {
\$sum2 += \$sum[\$place];
\$rank[\$couple]=array(\$place,-array_sum(\$total),\$sum2-\$total[++\$place]);
\$total2 += \$total[\$place];
if ( \$total2 > 3) {
break;
}
}

I'm not sure if the \$total reference in perl is total of array contents or total in array. I'll check it out.
0

Author Comment

ID: 13972523
Will try again tonight.
0

Author Comment

ID: 14051692
I'm stuck again. The function I am trying to convert was originally asked in

http://experts-exchange.com/Miscellaneous/Math_Science/Q_21366761.html

My perl is not as good as I thought. I don't understand all the code.

Can anyone convert to php function? Raising points.
0

Author Comment

ID: 14133791
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logoā¦
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will wā¦
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and ā¦
###### Suggested Courses
Course of the Month14 days, 9 hours left to enroll