User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #11  
Mar 26th, 2008
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:

  1. use strict;
  2.  
  3. my $openCount;
  4. my $closeCount;
  5. my $foundReturn;
  6. my $myCfile;
  7.  
  8. die "usage: perl $0 <filename>\n"
  9. if (!($ARGV[0]));
  10.  
  11. die "file: <",$ARGV[0],"> does not exist\n"
  12. if (! -e ($myCfile = $ARGV[0]));
  13.  
  14. open(fp,$myCfile);
  15. open(writeme,">output.txt");
  16.  
  17. print "reading <",$myCfile,"> ...\n";
  18.  
  19. $openCount = $closeCount = $foundReturn = 0;
  20. while(<fp>)
  21. {
  22. if ($_ =~ /return/) {
  23. print "hey am inside return if\n";
  24. $foundReturn=1;
  25. }
  26. elsif(($_ =~ /^\{/) || ($_ =~ /\{$/)) {
  27. $openCount++;
  28. print "openCount1=", $openCount ,"\n";
  29. }
  30. elsif(($_ =~ /^\}/) || ($_=~ /\}$/ )) {
  31. $closeCount++;
  32. print "closeCount=", $closeCount,"\n";
  33. }
  34. if($openCount==$closeCount) {
  35. print "count is equal\n";
  36. }
  37. }
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:
  1. int main (int argc, char *argv[])
  2. {
  3. unsigned *tempString, *tempPtr;
  4. unsigned i=0, opers=0, ptr=0, key;
  5. FILE *fp;
  6. typedef struct Operator_s {
  7. char Name[26];
  8. char Oin[11];
  9. char Password[16];
  10. } Operator_t;
Last edited by jephthah : Mar 26th, 2008 at 5:28 pm.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #12  
Mar 27th, 2008
in case it's not clear...

your parsing method will count the opening brace in line 6, but not the closing brace in line 10
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #13  
Mar 28th, 2008
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.
Last edited by rayscosmic : Mar 28th, 2008 at 3:14 am.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #14  
Mar 28th, 2008
(1) do not just blindly replace = with == , there was only one place where this was an issue

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.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #15  
Mar 28th, 2008
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 (\")
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #16  
Apr 8th, 2008
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.
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: add text in the c functions.plz help me

  #17  
Apr 8th, 2008
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.
Last edited by katharnakh : Apr 8th, 2008 at 7:23 am.
challenge the limits
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #18  
Apr 8th, 2008
hey thank u very much for ur response.My pgm is working fine with the path
$path="C:/Documents and Settings/bindiya.d/Desktop/example/context/model";
Thanku very much for helping me out
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: add text in the c functions.plz help me

  #19  
Apr 8th, 2008
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)
Last edited by jephthah : Apr 8th, 2008 at 11:03 am.
Reply With Quote  
Join Date: Mar 2008
Posts: 11
Reputation: rayscosmic is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rayscosmic rayscosmic is offline Offline
Newbie Poster

Re: add text in the c functions.plz help me

  #20  
Apr 9th, 2008
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 10:37 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC