How to rewrite this Java code in Perl?

Posted on 2011-10-24
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
    LVL 14

    Expert Comment

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

    egrep '[sSD]' FILE1 FILE2 > RESULTFILE


    Author Comment

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

    How can it be done?

    LVL 14

    Expert Comment

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

    egrep '[sSD] ' FILE1 FILE2 > RESULTFILE

    LVL 26

    Accepted Solution

    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 26

    Expert Comment

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

    Expert Comment

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    This video discusses moving either the default database or any database to a new volume.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now