[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to rewrite this Java code in Perl?

Posted on 2011-10-24
Medium Priority
Last Modified: 2012-05-12
I have this code in Java. I would like to rewrite it in Perl. How can I do it?

This code basically, opens a file, and if the line starts with s, S or D then we capture the entire line. and keep it in one single variable. So each match will be appended to the previous one with new line character.

		   # public static java.lang.StringBuilder createSubmittedFiles(java.lang.String pathToJobfile){
		   # java.lang.StringBuilder submittedFiles = new StringBuilder();
				# //find the submitted files in the job file 	
		   # Scanner inJob = null;
				# try{
					# inJob = new Scanner(new File(pathToJobfile));
					# java.lang.String jobFile = null;
					# while(inJob.hasNextLine()){
						# jobFile = inJob.nextLine();
						# if(jobFile.startsWith("s ") || jobFile.startsWith("S ") || jobFile.startsWith("D ")){
						# // This is to place an extra space between the file and the revision number
						# jobFile = jobFile.replaceAll("(\\.[A-Za-z]{1,3}) (\\d)", "$1  $2");
						# submittedFiles.append(jobFile).append("\n");
						# }
					# }
				# } 
				# catch(Exception e){
					# logger.error(e);
				# }
				# finally{
					# inJob.close();
				# }
		   # return (submittedFiles);
	   # } //createSubmittedFiles

Open in new window

Question by:Tolgar
  • 3
  • 2
LVL 14

Expert Comment

ID: 37020798
Wouldn't it be much simpler to simply grep for the lines starting with S, s, or D?



Author Comment

ID: 37020861
right. But there should a space after s, S or D.

How can it be done?

LVL 14

Expert Comment

ID: 37020940
If you want a space after it, you can just include it in the egrep pattern within the quotes, like this:


Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 27

Accepted Solution

wilcoxon earned 2000 total points
ID: 37020941
This is how I would do it...
sub createSubmittedFiles {
    my ($pathToJobFile) = @_;
    my @submittedFiles;
    open IN, $pathToJobFile or die "could not open $pathToJobFile: $!";
    while (<IN>) {
        if (m{^[sSD]\s+}) {
            # this appears to match the substitution regex in the Java code
            # but I don't see that it accomplishes anything
            s{(\.[A-Za-z]{1,3}) (\d)}{$1 $2}g;
            push @submittedFiles, $_;
    # if called in list context, return a list of lines
    # if called in scalar context, return a \n separated string of lines
    return wantarray ? @submittedFiles : join("\n", @submittedFiles);

Open in new window

LVL 27

Expert Comment

ID: 37020950
sentner's egrep will work as well but the question asked for perl code (so that's what I provided).
LVL 14

Expert Comment

ID: 37020957
One typo in my egrep BTW.. I neglected the carat at the beginning to signify the line has to start with the s, S, or D...

egrep '^[sSD] ' FILE1 FILE2 > RESULTFILE

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question