#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Conversion of Perl script to Java


    Okay guys, so I am doing an extra credit assignment at my university in my intro to data structures class and the goal is to write a program that when fed a file(s) in the terminal it will format the file to a certain word length and wrap the words, write it to stdout, which will have it formatted properly. The default word length is 65 per line, and the man synopsis for the file is the following:

    jfmt [-width][filename...]

    For a better explanation of the purpose this is the general summary of the program that the professor listed on the EC assignment page:

    "The format utility reads in text lines from all of its input files and writes its output to stdout.
    Error messages are written to stderr. Each file is handled separately, and within each file, all
    consecutive sequences of lines containing non-whitespace characters are considered as a single
    paragraph. A paragraph is written out with a maximal sequence of words not to exceed the
    specified output line width. It is then followed by one empty line."

    The Perl script was given, and it does exactly what the program should do in Java, problem is I have never used Perl before and am not an expert in Java to easily be able to look at other languages and quickly convert them. Can anybody help me out? I know it seems pretty easy, but I am having a hard time figuring out how to expand on the Java starter code given. Below is the Perl script, and below that is the Java starter code I was given.

    Perl script:
    PHP Code:
    #!/usr/bin/perl
    # $Id: pfmt.perl,v 1.1 2013-09-24 14:41:47-07 - - $
    use strict;
    use 
    warnings;

    $
    =~ s|^.*/||;
    my $exit_status 0;
    END {exit $exit_status}
    sub note(@) {print STDERR "@_"};
    $SIG{'__WARN__'} = sub {note @_$exit_status 1};
    $SIG{'__DIE__'} = sub {warn @_; exit};

    my $linelen 65;
    if (@
    ARGV and $ARGV[0] =~ m/^-(.+)/) {
       
    $linelen = $1;
       die 
    "Usage: $0 [-width] [filename...]\n" if $linelen =~ m/\D/;
       
    shift @ARGV
    }

    sub print_paragraph (@) {
       
    my (@words) = @_;
       print 
    "\n";
       
    my $char_count 0;
       for 
    my $word (@words) {
          if (
    $char_count == 0) {
             print 
    $word;
             
    $char_count length $word;
          }else {
             
    $char_count += length $word;
             if (
    $char_count $linelen) {
                print 
    "\n"$word;
                
    $char_count length $word;
             }else {
                print 
    " "$word;
             }
          }
       }
       print 
    "\n" if $char_count 0;
    }

    push @ARGV"-" unless @ARGV;
    for 
    my $filename (@ARGV) {
       
    open my $file"<$filenameor warn "$0: $filename: $!\n" and next;
       
    my @output_words;
       for (;;) {
          
    my $input_line = <$file>;
          
    last unless defined $input_line;
          
    my @input_words split " "$input_line;
          if (@
    input_words) {
             
    push @output_words, @input_words;
          }else {
             
    print_paragraph @output_words if @output_words;
             @
    output_words = ();
          }
       }
       
    print_paragraph @output_words;
       
    close $file;

    Java starter code:
    Code:
    // $Id: jfmt.java,v 1.2 2013-09-24 14:38:16-07 - - $
    //
    // Starter code for the jfmt utility.  This program is similar
    // to the example code jcat.java, which iterates over all of its
    // input files, except that this program shows how to use
    // String.split to extract non-whitespace sequences of characters
    // from each line.
    //
    
    import java.io.*;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Scanner;
    import static java.lang.System.*;
    
    class jfmt {
       // Static variables keeping the general status of the program.
       public static final String JAR_NAME = get_jarname();
       public static final int EXIT_SUCCESS = 0;
       public static final int EXIT_FAILURE = 1;
       public static int exit_status = EXIT_SUCCESS;
    
       // A basename is the final component of a pathname.
       // If a java program is run from a jar, the classpath is the
       // pathname of the jar.
       static String get_jarname() {
          String jarpath = getProperty ("java.class.path");
          int lastslash = jarpath.lastIndexOf ('/');
          if (lastslash < 0) return jarpath;
          return jarpath.substring (lastslash + 1);
       }
    
    
       // Formats a single file.
       static void format (Scanner infile) {
          // Read each line from the opened file, one after the other.
          // Stop the loop at end of file.
          for (int linenr = 1; infile.hasNextLine(); ++linenr) {
             String line = infile.nextLine();
             out.printf ("line %3d: [%s]%n", linenr, line);
    
             // Create a LinkedList of Strings.
             List<String> words = new LinkedList<String>();
    
             // Split the line into words around white space and iterate
             // over the words.
             for (String word: line.split ("\\s+")) {
    
                // Skip an empty word if such is found.
                if (word.length() == 0) continue;
                out.printf ("...[%s]%n", word);
                // Append the word to the end of the linked list.
                words.add (word);
    
             }
             out.printf ("list:");
    
             // Use iterator syntax to print out all of the words.
             for (String word: words) out.printf (" %s", word);
             out.printf ("%n");
          }
       }
    
    
       // Main function scans arguments and opens/closes files.
       public static void main (String[] args) {
          if (args.length == 0) {
             // There are no filenames given on the command line.
             out.printf ("FILE: -%n");
             format (new Scanner (in));
          }else {
             // Iterate over each filename given on the command line.
             for (int argix = 0; argix < args.length; ++argix) {
                String filename = args[argix];
                if (filename.equals ("-")) {
                   // Treat a filename of "-" to mean System.in.
                   out.printf ("FILE: -%n");
                   format (new Scanner (in));
                }else {
                   // Open the file and read it, or error out.
                   try {
                      Scanner infile = new Scanner (new File (filename));
                      out.printf ("FILE: %s%n", filename);
                      format (infile);
                      infile.close();
                   }catch (IOException error) {
                      exit_status = EXIT_FAILURE;
                      err.printf ("%s: %s%n", JAR_NAME,
                                  error.getMessage());
                   }
                }
             }
          }
          exit (exit_status);
       }
    
    }
    Hopefully you guys can help me with this!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Can anybody help me out? I know it seems pretty easy
    You really do believe someone is eager to do your assignment instead of you?

    And by the way it does not seem pretty easy. Languages are different enough and not many people are proficient enough in both so it is easier to write new program given verbal description of functionality provided by your perl script. If you have any.

    Though I do not think anyone will care to do this for you completely. You'd better start yourself and when you have any specific question - feel free to ask.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    I'm not asking for somebody to do this for me, I am asking for somebody to help me get started. I am not sure where to begin.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    where to begin.
    Do the project in steps:
    format utility reads in text lines from all of its input files and writes its output to stdout.
    There are three steps described there:
    1) parse list of input files
    2) read from each file
    3)write output

    Do the first step: parse the list of input files and for debug: print the file names

    When that step is working, move to the next step.

IMN logo majestic logo threadwatch logo seochat tools logo