Porting Perl to PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stefko
    New Member
    • Dec 2006
    • 3

    Porting Perl to PHP

    After a full day of attemping to port this PERL routine to PHP, I have given up on being able to do this myself, can anybody help?

    Code:
    for($y=1;$y<=100;$y++){
    		$thename="NAME_$y";
    			if(length(${$thename})>3){
    			$num++;
    		}
    	}
    	foreach($x=1; $x<=$num; $x++){
    		$qnt="QUANTITY_$x";
    		$prce="PRICE_$x";
    		$xtnd="XTEND_$x";
    		$ide="ID_$x";
    		$naam="NAME_$x";
    		$adlinfo="ADDTLINFO_$x";
    		${$naam}=~ s/\n//g;
    $strMessageBody .= "Qty: ${$qnt}   \nItem No: ${$ide}   \nDescpition: ${$naam}   \nAddtl. Info: ${$adlinfo}   \nPrice: \$${$prce}      Extended Price: \$${$xtnd} \n\n";
    	}
  • steven
    New Member
    • Sep 2006
    • 143

    #2
    Can you show us what you have so far in PHP?

    I'm sure people will be willing to help if you show what you have attempted so far and perhaps we can help with any specific problems you have. Do you receive error messages? Does the script work but not as intended? More information is always handy. What's the aim of the script?

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      I assume you are making a list of labels and associated variable names. The first part of your code is, however, unclear as to what its function should be, except for setting the $num variable.. Please explain what you intend to accomplish there.

      The second part can be coded, when I am right in my label/variable name assumption, as follows (I also commented the reg expression out, because I have no idea what you want to do there):
      [php]$num=100; // assumed end-value of part 1 of the code
      $strMessageBody ='';
      for($x=1; $x<=$num; $x++){
      $qnt="QUANTITY_ $x";
      $prce="PRICE_$x ";
      $xtnd="XTEND_$x ";
      $ide="ID_$x";
      $naam="NAME_$x" ;
      $adlinfo="ADDTL INFO_$x";
      /* ${$naam} =~ s/\n//g; */
      $strMessageBody .= "Qty: \${$qnt}
      <br />Item No: \${$ide}
      <br />Descpition: \${$naam}
      <br />Addtl. Info: \${$adlinfo}
      <br />Price: \${$prce}
      <br />Extended Price: \${$xtnd} <br />";
      }
      echo $strMessageBody ;
      [/php]
      Ronald :cool:

      Comment

      • Stefko
        New Member
        • Dec 2006
        • 3

        #4
        Thanks that looks like it would work, tried something close to that but to no avail.

        That first section of code I realized can be dropped all together!

        After another day of sweating out code, trial and error, (lots of error) I got it to work.
        Here is what I came up with, tested and works!
        The var $cartMax is set earlier in the script

        [PHP] for ( $x = 1; $x <= $cartMax; $x++) {
        $qnt=$_REQUEST['QUANTITY_'.$x];
        if ($qnt == '') {
        break;
        }
        $prce=$_REQUEST['PRICE_'.$x];
        $xtnd=$_REQUEST['XTEND_'.$x];
        $ide=$_REQUEST['ID_'.$x];
        $naam=$_REQUEST['NAME_'.$x];
        $adlinfo=$_REQU EST['ADDTLINFO_'.$x];
        //$naam=~ s/\n//g;

        $strMessageBody .= "Qty: $qnt \nItem No: $ide \nDescpition: $naam \nAddtl. Info: $adlinfo \nPrice: \$$prce Extended Price: \$$xtnd \n\n";
        }[/PHP]

        Comment

        • Stefko
          New Member
          • Dec 2006
          • 3

          #5
          Again thats for the assist.

          the original PERL code was written by a 16yr old, and worked great, admittedly the code was poorly written!

          Sorry, i should have stated that I was intending to retreive POST vars.

          Comment

          Working...