Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to count number of multiple logical conditions used in if,elseif or while in perl

by loki (Novice)
on Dec 21, 2009 at 13:37 UTC ( [id://813700]=perlquestion: print w/replies, xml ) Need Help??

loki has asked for the wisdom of the Perl Monks concerning the following question:

hi all...i am newbie to perl...i have a while,if,elseif statements in a file with multipe conditions inside it... it is a C language...the format is mentioned below is standard for all the multiple conditions.So no worries about the indendation.The only problem is to check how many conditions are there and list as per output format that i have described.... eg if my C file have a code...
while( condition1 && condition2 || condition3 ) { #statements; }
i want to count how many conditions are there inside the while and my output should be of like this...
while( 1 condition1 && 2 condition2 || 3 condition3 ) { #statements; }
i have written the code and it works fine for simple one.. my code....
open(A,"e:\\a\\a.txt")or die; @a=<A>; close(A); $count=1; for($i=0;$i<scalar@a;$i++) { if($a[$i]=~m/while/g) { $line=$i; until($a[$line]=~/\{/g) { if($a[$line]=~/(.*)[\&&\||]/g){print"$count $a[$line]";$count++ +;} elsif($a[$line]=~/\(.*\)[\&&\||]/g){print"$count $a[$line]";$co +unt++;} else{print$a[$line];} $line++; } } last if($a[$line]=~/\{/g); }
but for complicated conditions like
while( ( condition1 && condition2 && condition3 ) || ( condition4 || condition5 && condition6 ) { #statements; }
am getting the output like
while( ( 1 condition1 && 2 condition2 && condition3 3 ) || ( 4 condition4 || 5 condition5 && condition6 ) )
which is not desired.... my intension is to count all the conditions regarding however complicated it is..... please help me... desired output may be
while( ( 1 condition1 && 2 condition2 && 3 condition3 ) || ( 4 condition4 || 5 condition5 && 6 condition6 ) )
since it has used 6 conditions inside... hence forth for any cases... please help me .....

Replies are listed 'Best First'.
Re: How to count number of multiple logical conditions used in if,elseif or while in perl
by Marshall (Canon) on Dec 21, 2009 at 16:51 UTC
    Your code formatting is difficult to read. I just took at stab at this question based upon what I could understand of the problem statement and your input/output data. In Perl, you can have an executable statement as part of a regex substitution and I demo that below. I use a global substitution with an executable part to count the number of conditions.

    This code is of course not going to parse some arbitrary C code "while" statement lines(s)! It does appear to execute your test case as presented and I hope a starting point for you for further refinement. Of course this Perl code assumes that you have at least compilable C code.

    Curiosity gets the better of me and I'm wondering what the application is? If it is some kind of code complexity measurement, perhaps the number of && and || operators would be more to what you want?

    UPDATE: Made some small revisions to previous code. After seeing your post with an example and desired output.

    #!/usr/bin/perl -w use strict; my $line; while ($line = <DATA>) { if ($line =~ m/\s*while/) { process_while(); next; } print $line; } sub process_while { #tweaked to allow the "(" after "while" to be on next line #or have blank lines before the "(" #that's for this for loop does find the first "(" line for ( ;$line !~ /\(/;(print $line),$line=(<DATA>)){} for (my $n_cndx =1; $line !~ m/\{/ ; $line=(<DATA>) ) { $line =~ s/(\w+)(\s*)(&&|\|\||\s*\)|\s*\n)/ $n_cndx++." $1$2$3" +/ge; print $line; } } =Prints: while ( ( 1 condition1 && 2 condition2 && 3 condition3 ) || ( 4 condition4 || 5 condition5 && 6 condition6 ) ) {more statements}; while(1 X){} =cut __DATA__ while ( ( condition1 && condition2 && condition3 ) || ( condition4 || condition5 && condition6 ) ) {more statements}; while(X){}
    older code is here, but not much different.
Re: How to count number of multiple logical conditions used in if,elseif or while in perl
by Anonymous Monk on Dec 21, 2009 at 14:06 UTC
    This is so unreadable, please take a look at Markup in the Monastery , Perl Monks Approved HTML tags, and then, right beneath the text box where you wrote your question you have clear instructions there to remind you of what you're required to do..

    Click on the link to your question once again and you'd be able to re-edit..this way you would guarantee that a response (not an angry one of course) is garnered to your questions

      sorry i have corrected it ... i am a first time user..

        I updated my code below based on your updated and much better formatted requirements. Thanks for doing that!

        You should be aware that something like this [\&&\||] doesn't do what I think you think it does! This is a character set with "& and |". double && can't be specified that way.

        The main problem I see is an overly complex looping and program structure. My code to deal with the "while" has one loop statement (a for loop), one regex and one print. Your code goes 4 levels deep with complex if statements. I did put an extra "for" to skip lines in case "(" wasn't on the same line as the "while", but that is just gravy.

        Glad to help you. Is this some sort of homework assignment? It is ok with me if it is, but I'd like to know that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://813700]
Approved by broomduster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found