•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 374,006 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,870 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 1565 | Replies: 23 | Solved
![]() |
1) i noticed in your earlier code you were using = when you should have been using ==
2) you also had a do statement without a corresponding while condition. avoid do/while's for this and other reasons. you should stick with "while" loops unless you have compelling reason to use "do/while"
3) you should not be "return"-ing in the "else" statement.
4) you should not attempt to define the end of a function by the appearance of "return"... besides the fact that void types do not return a value, a function can have any number of return statements.
otherwise, this sort of works in a basic sense:
you STILL have the problem that if a brace is both preceded and followed by any combination of whitespace, comments, or additional code -- which is a VERY likely prospect -- your counting code will become immediately and irretreivably lost.
for instance:
2) you also had a do statement without a corresponding while condition. avoid do/while's for this and other reasons. you should stick with "while" loops unless you have compelling reason to use "do/while"
3) you should not be "return"-ing in the "else" statement.
4) you should not attempt to define the end of a function by the appearance of "return"... besides the fact that void types do not return a value, a function can have any number of return statements.
otherwise, this sort of works in a basic sense:
perl Syntax (Toggle Plain Text)
use strict; my $openCount; my $closeCount; my $foundReturn; my $myCfile; die "usage: perl $0 <filename>\n" if (!($ARGV[0])); die "file: <",$ARGV[0],"> does not exist\n" if (! -e ($myCfile = $ARGV[0])); open(fp,$myCfile); open(writeme,">output.txt"); print "reading <",$myCfile,"> ...\n"; $openCount = $closeCount = $foundReturn = 0; while(<fp>) { if ($_ =~ /return/) { print "hey am inside return if\n"; $foundReturn=1; } elsif(($_ =~ /^\{/) || ($_ =~ /\{$/)) { $openCount++; print "openCount1=", $openCount ,"\n"; } elsif(($_ =~ /^\}/) || ($_=~ /\}$/ )) { $closeCount++; print "closeCount=", $closeCount,"\n"; } if($openCount==$closeCount) { print "count is equal\n"; } }
for instance:
c Syntax (Toggle Plain Text)
int main (int argc, char *argv[]) { unsigned *tempString, *tempPtr; unsigned i=0, opers=0, ptr=0, key; FILE *fp; typedef struct Operator_s { char Name[26]; char Oin[11]; char Password[16]; } Operator_t;
Last edited by jephthah : Mar 26th, 2008 at 5:28 pm.
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Hey thank for taking ur time and helping me out.
1)I have replaced = with == where ever nesessary.
2) Few functions does not return any value for those ,to check the end of the functions i am taking brace count.
3) a) if(..) { // comment
b) else(..) } //comment
For cases like a) and b) i dont know what to do as the way i am checking wil not workout
4) Ex1: if(...) {
i dont know why but the below condition does not holds true in case of Ex1.
elsif(($_ =~ /^\{/) || ($_ =~ /\{$/))
5)There is always a curser pbm like if i am checking for return in the function(there can b multiple returns in a single function) line wil be inserted after return as curser wil be in the return statment.But it has to go back and insert line before return. Even the same case when i insert line at the start of the function.
1)I have replaced = with == where ever nesessary.
2) Few functions does not return any value for those ,to check the end of the functions i am taking brace count.
3) a) if(..) { // comment
b) else(..) } //comment
For cases like a) and b) i dont know what to do as the way i am checking wil not workout
4) Ex1: if(...) {
i dont know why but the below condition does not holds true in case of Ex1.
elsif(($_ =~ /^\{/) || ($_ =~ /\{$/))
5)There is always a curser pbm like if i am checking for return in the function(there can b multiple returns in a single function) line wil be inserted after return as curser wil be in the return statment.But it has to go back and insert line before return. Even the same case when i insert line at the start of the function.
Last edited by rayscosmic : Mar 28th, 2008 at 3:14 am.
(1) do not just blindly replace = with == , there was only one place where this was an issue
(2) SOME functions can have NO "return" statements (void) ... but ANY function can have MANY returns (even void). you can NOT rely on the existence of "return" statements. if you try to, you will inevitably fail.
3) that is the very problem ive been telling you about. the method behind your choice of regex is flawed. this is the root of your problem. you need to think about another pattern by which you can find open and close braces without relying the "Beginning of Line" and "End of Line" operators (^ and $)
4) you probably have some whitespace (one or more spaces and/or tabs) after the brace. this is the same problem that is plaguing you in 3)
5) i dont understand what you mean
if($openCount==$closeCount)(2) SOME functions can have NO "return" statements (void) ... but ANY function can have MANY returns (even void). you can NOT rely on the existence of "return" statements. if you try to, you will inevitably fail.
3) that is the very problem ive been telling you about. the method behind your choice of regex is flawed. this is the root of your problem. you need to think about another pattern by which you can find open and close braces without relying the "Beginning of Line" and "End of Line" operators (^ and $)
4) you probably have some whitespace (one or more spaces and/or tabs) after the brace. this is the same problem that is plaguing you in 3)
5) i dont understand what you mean
Last edited by jephthah : Mar 28th, 2008 at 9:08 am.
heres some more hints:
(1) do not attempt to look for "return" statements. at all.
(2) only use the open/close brace counting method.
(3) you need to change your regex method. merely looking for a brace at the beginning or end of the line WILL NOT cut it -- as you have already experienced.
(4) instead, look for ANY AND ALL braces, with the EXCEPTION of braces that are found within quotations (ie, braces to be printed.
this means that you can not just merely look for and count braces.... you have to look for and keep track of quotation marks ("), but disregard escaped quotation marks that are themselves being printed (\")
(1) do not attempt to look for "return" statements. at all.
(2) only use the open/close brace counting method.
(3) you need to change your regex method. merely looking for a brace at the beginning or end of the line WILL NOT cut it -- as you have already experienced.
(4) instead, look for ANY AND ALL braces, with the EXCEPTION of braces that are found within quotations (ie, braces to be printed.
this means that you can not just merely look for and count braces.... you have to look for and keep track of quotation marks ("), but disregard escaped quotation marks that are themselves being printed (\")
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Hey thanks a lot for your help. Now my perl pgm is working. I want to try this pgm in windows. i have installed perlIDE and DzSoft perl editor. As i was giving the file path as command line argument in linex i don’t know how to give it in windows. i tried giving direct path in the pgm but its not working. can u just let me know how to give the path
I tried with this(context is the folder name which contains .c and .h files)
$path=\context;
$path="\context";
$path =C:\Documents and Settings\bindiya.d\Desktop\example\context;
Its not taking the path in this way.
I tried with this(context is the folder name which contains .c and .h files)
$path=\context;
$path="\context";
$path =C:\Documents and Settings\bindiya.d\Desktop\example\context;
Its not taking the path in this way.
replace '\'(single slash) with '\\' double slash for proper intrepretation. Because '\' is a escape character in Perl.
$path = "C:\\Documents and Settings\\bindiya.d\\Desktop\\example\\context";
which is nothing but C:\Documents and Settings\bindiya.d\Desktop\example\context
kath.
$path = "C:\\Documents and Settings\\bindiya.d\\Desktop\\example\\context";
which is nothing but C:\Documents and Settings\bindiya.d\Desktop\example\context
kath.
Last edited by katharnakh : Apr 8th, 2008 at 7:23 am.
challenge the limits
glad you got it figured out 
for Perl in Windows, i highly recommend that you use ActiveState Perl. It's the industry standard, and has a ton of modules and great community support.
http://www.activestate.com/solutions/perl/
they sell development kits, IDE's and whatnot, but the basic distribution is free (Active Perl)

for Perl in Windows, i highly recommend that you use ActiveState Perl. It's the industry standard, and has a ton of modules and great community support.
http://www.activestate.com/solutions/perl/
they sell development kits, IDE's and whatnot, but the basic distribution is free (Active Perl)
Last edited by jephthah : Apr 8th, 2008 at 11:03 am.
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Thanks a lot. I am sorry to bother u again but i still have a small pbm with my script.
1)i want to eleminate comment lines being processed.For exp
// if (..) {
above line should not be processed. i tryed with this
if ($_ =~ / \ / \ / /)
but its not working.
2) And evevn i want to remove blank line but noe able to know how to do that
Let me know where i am going wrong.
Thanks and regards
1)i want to eleminate comment lines being processed.For exp
// if (..) {
above line should not be processed. i tryed with this
if ($_ =~ / \ / \ / /)
but its not working.
2) And evevn i want to remove blank line but noe able to know how to do that
Let me know where i am going wrong.
Thanks and regards
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
Similar Threads
Other Threads in the Perl Forum
- Previous Thread: getting 401 Unauthorized from an xml feed...
- Next Thread: count characters in a string


Linear Mode