Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
transform If-then-else statemt to ?: opeator statement



hmida
New User

Nov 11, 2010, 10:15 AM


Views: 5380
transform If-then-else statemt to ?: opeator statement

Hi everybody,

im searching for a way to transform ,
an if-then-else-fi statement, to an conditional operator one.

i mean, a script that transform thos code:
if Cond then Exp else Exp2 fi
to :
(Cond) ? (Exp) : (Exp2)

thanks in advance.


Zhris
Enthusiast

Nov 12, 2010, 3:32 AM


Views: 5372
Re: [hmida] transform If-then-else statemt to ?: opeator statement

Hi,

Do all the conditional blocks have a regular format, or does it vary e.g.


Code
if ($true) {      
$name = 'Peter';
}

is different from:

Code
if($true){$name='Peter';}

And is even more different from:

Code
if ((!defined $hash{$key}) and ($hash{'default'} == 9)) {    
$name = 'Peter';
$age = 40;
}


Chris


(This post was edited by Zhris on Nov 12, 2010, 3:38 AM)


hmida
New User

Nov 12, 2010, 4:11 AM


Views: 5364
Re: [Zhris] transform If-then-else statemt to ?: opeator statement

Thanks fo you intereset,
No they vay:
if ($a==1){
$b=1;
}else{
if ($a==2){
$b=3;
}
}

'else' is optionale,
'if' can be imbricated !


Zhris
Enthusiast

Nov 12, 2010, 5:21 AM


Views: 5360
Re: [hmida] transform If-then-else statemt to ?: opeator statement

Hi,

Firstly what is your reason for doing this. The operator method is less readable than your original statements. Also, would it not be quicker to go through and do this by hand, unless of course you are dealing with hundreds or thousands of changes. It may take a while to write a script which performs your task to a suitable degree of accuracy (100%) if the statements aren't regular throughout.

Chris


(This post was edited by Zhris on Nov 12, 2010, 5:23 AM)


hmida
New User

Nov 12, 2010, 8:05 AM


Views: 5352
Re: [Zhris] transform If-then-else statemt to ?: opeator statement

thanks for the reply,

i want to change codes like this:

<if condition="$vboptions[tosurl]">
<a href="$vboptions[tosurl]">$vbphrase[terms_of_service]</a> -
</if>
...
<if condition="$show['deletedpost']">
\n\t\t\t
<if condition="$show['managepost']">
\n\t\t\t\t<a href="postings.php?$session[sessionurl]do=managepost&amp;p=$post[postid]"><img src="$stylevar[imgdir_misc]/trashcan.gif" alt="$vbphrase[manage]" border="0" /></a> \n\t\t\t
<else />
\n\t\t\t\t<img src="$stylevar[imgdir_misc]/trashcan.gif" alt="$vbphrase[deleted_post]" border="0" /> \n\t\t\t
</if>
\n\t\t
</if>
\n\t\t
...



to code like this :
<tr>
".(($show['quickchooser']) ? ("
<td class=\"tfoot\">
<select name=\"styleid\" onchange=\"switch_id(this, 'style')\">
<optgroup label=\"$vbphrase[quick_style_chooser]\">
$quickchooserbits
</optgroup>
</select>
</td>
") : (""))."
".(($show['languagechooser']) ? ("
<td class=\"tfoot\">
<select name=\"langid\" onchange=\"switch_id(this, 'lang')\">
<optgroup label=\"$vbphrase[quick_language_chooser]\">
$languagechooserbits
</optgroup>
</select>
</td>
") : (""))."
...


i m doing this cause, it's requied
in the syntaxe of the program, im writing code for.


Zhris
Enthusiast

Nov 12, 2010, 9:59 AM


Views: 5350
Re: [hmida] transform If-then-else statemt to ?: opeator statement

Alright, ill have to have a think on this 1, could probably use a simple search and replace regex, but ill have to look carefully over your example data.

Chris


hmida
New User

Nov 12, 2010, 11:21 AM


Views: 5347
Re: [Zhris] transform If-then-else statemt to ?: opeator statement

ok,
thank you fo your suggestions.

i will try .
thanks