LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-28-2014, 10:15 PM   #1
nickygencs17
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Rep: Reputation: Disabled
Lightbulb perl scripting question


hey guys need a little bit of help on this scripting project i have...

file

// handling args in C++

//include for cout
#include <iostream>
//to parse ints
#include <sstream>

//make the use of cout even easier
using namespace std;

/* Add 2 to integer encoded as char string, if invalid string, treat as 0 */
int add2(char* argv)
{
istringstream iss(argv);
int v;
if (iss >> v)
return v+2;
else
return 2; //invalid int, so treat as 0 + 2
}

/*
* this is the main function
* and has a multi-line comment before the function */
int main(int argc, char* argv[])
{
/*unsigned*/ int sum = 0;
cout << argc-1 << " arg(s) given." << endl; //Output number of args given
//Loop around each argument
for (int i = 1; i < argc; ++i) {
sum += add2(argv[i]); //Add int val of arg, plus 2
}
cout << "//Weird sum of args = " << sum << endl;
return 0; //return success
}


desired output


#include <iostream>
#include <sstream>

using namespace std;

int add2(char* argv)
{
istringstream iss(argv);
int v;
if (iss >> v)
return v+2;
else
return 2; //invalid int, so treat as 0 + 2
}

int main(int argc, char* argv[])
{
int sum = 0;
cout << argc-1 << " arg(s) given." << endl; //Output number of args given
for (int i = 1; i < argc; ++i) {
sum += add2(argv[i]); //Add int val of arg, plus 2
}
cout << "//Weird sum of args = " << sum << endl;
return 0; //return success
}

as you can tell I'm missing a space after using namespace and before #include<iostream>
id like not just put them in there but modify the code so it works for other scripts, i really appreciate all the help and comments thank you!

Code:
#!/usr/bin/perl -w 

while(<>)#starts while loop 
{
        if(s/^\s*\/\///) #if line starts with // do nothing 
        {
        }
        elsif(s/(\s*)\/\*.*\//$1/) #if line starts with /* and ends with */ replace it with a tab
        {
        print "$_";
        }
        else #else statement for multiline comments and everything else 
        {
        BEGIN {  my $ccom = 0; } #begin executes code below


                if ( s|/[*].*$|| )
                {                # start comment
                print if /./;    # might be something before '/*'
                $ccom = 1;
                }
                if ( s|.*[*]/||  )
                {        # end comment
                $ccom = 0;
                }
                if (!/^$/)
                {
                print if !$ccom; #print if not equal to 0 or 1
                }
        }
}
 
Old 04-29-2014, 10:00 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,667

Rep: Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710
This test will skip any empty lines in your file:

Code:
if (!/^$/)
 
Old 04-29-2014, 06:54 PM   #3
nickygencs17
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
nevermind solved

Code:
#!/usr/bin/perl -w 
#Nicholas Genco
#OPENING LINE  

while(<>)#starts while loop 
{
        if(/^\s*\/\//) #if line starts with // do nothing 
        {
        }
        elsif(s/(\s*)\/\*.*\//$1/) #if line starts with /* and ends with */ replace it with a tab
        {
                if (!/^$/) #if line is not empty print it
                {
                print "$_";
                }
        }
        elsif( /^\s*\/\*([^\*][^\/])*/) #if a comment starts with /*
        {
                $ccom = 1;
                while (<> && $ccom == 1) #while loop to find comment end */
                {
                        if (/.*\*\/*/) #if */ is found exit while loop
                        {
                        $ccom = 0;
                        }
                }
         }
        else #if everything fails print "$_"
        {
        print "$_";
        }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl scripting question Quest ion Programming 10 11-13-2013 10:56 AM
perl scripting question Quest ion Programming 4 12-02-2011 04:39 AM
[SOLVED] Perl scripting question zeratul111 Linux - General 4 10-13-2010 04:53 PM
question about perl scripting feetyouwell Linux - Software 2 02-13-2004 04:11 PM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration