CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

Post #1 of 7 (2420 views)
transform If-then-else statemt to ?: opeator statement Can't Post

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

Post #2 of 7 (2412 views)
Re: [hmida] transform If-then-else statemt to ?: opeator statement [In reply to] Can't Post

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

Post #3 of 7 (2404 views)
Re: [Zhris] transform If-then-else statemt to ?: opeator statement [In reply to] Can't Post

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

Post #4 of 7 (2400 views)
Re: [hmida] transform If-then-else statemt to ?: opeator statement [In reply to] Can't Post

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

Post #5 of 7 (2392 views)
Re: [Zhris] transform If-then-else statemt to ?: opeator statement [In reply to] Can't Post

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

Post #6 of 7 (2390 views)
Re: [hmida] transform If-then-else statemt to ?: opeator statement [In reply to] Can't Post

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

Post #7 of 7 (2387 views)
Re: [Zhris] transform If-then-else statemt to ?: opeator statement [In reply to] Can't Post

ok,
thank you fo your suggestions.

i will try .
thanks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives