Link to home
Start Free TrialLog in
Avatar of waikap
waikap

asked on

help for a beginner!

I am a beginner of perl. I only know little bit about programming. I tried to write a perl program to cut up each paragraph into lines that has as many words as
possible. And the last line should be single spaced.


XXXXXXXXXXXXX.XXXXXXXXXX,XXXXXXXXXXX. XXXXXXXX.
XXXXXXXXXXXXX.XXXXXXXXXX,XXXXXXXXXXX. XXXXXXXX.


XXXXXXX. YYYYYY. YYYY,YYY. YYY,YYY. YYYY.  ZZZZ.ZZZZ.
XXXX,XXXX.
    ZZZZZZ.ZZZZZZZZZZZZZZZZZZZZZZZZZZ.ZZZZZZZZZZZZZ


And I will get the output to another file like that:
XXXXXXXXXXXXX.XXXXXXXXXX,XXXXXXXXXXX. XXXXXXXX.
XXXXXXXXXXXXX.XXXXXXXXXX,XXXXXXXXXXX.

XXXXXXX. YYYYYY. YYYY,YYY. YYY,YYY. YYYY.  ZZZZ.ZZZZ.XXXX,XXXX.

ZZZZZZ.ZZZZZZZZZZZZZZZZZZZZZZZZZZ.ZZZZZZZZZZZZZ


here is the code I tried on my program, however, it don't work at all. I tried to look for some perl books for help. However, I just know which is good for me. Also, if you know which perl book is good for beginner, just let me know.
--------------

#!/usr/bin/perl

 print "Input a file name:";
 $infilename=<STDIN>;
 chop ($infilename);
 open(IN, $infilename);

 print "Type output file name:";
 $outfilename=<STDIN>;
 chop ($outfilename);
  open(OUT, ">$outfilename");
  while(<IN>) {
 tr/\r//;  #remove all the "\r" from the input file.
 s/\n\s*\n////g; #replace empty lines with Tags
        s/\n+//g;  # remove newlines
        s/\t+////g;  # replace multiple tabs with nothing
        s/\n\n\t/\t/;  # remove new leading newline artifacts
        s/$/\n/;  # add newline at end

     print OUT $_;
  }
 close (IN);
 close (OUT);
Avatar of malec
malec

I don't understand what you are trying to do. Why would you remove endline (it is always at the end of the line) and then add it to each line? Also lines s/\n\s*\n////g; and s/\t+////g; will produce syntax error in  Perl 5.
Avatar of ozo
use Text::Wrap;
use Text::Wrap qw(wrap $columns);
$columns = 48;
$/="";
while( <> ){
 s/\s+/ /g;
 print wrap("","",$_),"\n\n";
}
a good book is:
"programming perl" O'Reilly & Ass, Inc

ASKER CERTIFIED SOLUTION
Avatar of haystor
haystor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
haystor, I think you typed += where you meant to say .=
Thank you, Ozo,

I'm sitting here studying Java as I am trying to program Perl.
Allow me to try again, this time cutting and pasting instead of retyping:

I have written this one assuming that each line should be turned into a paragraph.  I believe the variables can be followed easily.  Just put this subroutine into a program,  send it the columns, input file, and output file.  It should do the rest, as well as echo it back to STOUT.  You can comment that line out if needed.

If you meant that every paragraph should be a defined by a blank line, just send a comment, and I will show you where to put that.  Have fun

#!/usr/local/bin/perl


$COLUMNS = 30;
$infile = "text.txt";
$outfile = "out.txt";

WrapIt($COLUMNS, $infile, $outfile);  # once you have the above three values
                                      # just put the WrapIt line in.

sub WrapIt {
    ($COLUMNS, $infile, $outfile) = @_;
    open (IN, "$infile") or print "cannot open filename\n";
    @lines = <IN>;
    close IN;
    chomp @lines;
   
    @newlines = ();
    foreach $i (@lines) {
      @words = split(/\s+/, $i);
      if ($i eq "") {
          push(@newlines, "");
          next;
      }
      foreach $j (@words) {
          if ($COLUMNS < (length($currentline) + 1 + length($j))) {
            push (@newlines, $currentline);
            $currentline = "$j";
          } else {
            if ($currentline) {
                $currentline .= " $j";
            } else {
                $currentline = "$j";
            }
          }
      }
      push(@newlines, $currentline);
      $currentline = "";
    }
   
   
    open (OUT, ">$outfile") or print "cannot write to output file\n";
    foreach $i (@newlines) {
      print OUT "$i\n";
      print "$i\n";  # echoes to ST OUT
    }
}
Avatar of waikap

ASKER

Hi Laystor,

Thank you for your effort to help me out of this problem. I try both of your codes. The first one seems not working very well. It just give me all 0 in very lines. The second one is working. However, it just give somethink like the following. I don't if that is problem with the perl compiler I am using or what.(I am using Win32 perl for windows 95). I want to cut up each garagraph into lines that has as many words in each lines as possible. I use a entire blank line to determine a new paragraph. Maybe, it will be much clear, if I give an example.

Test file:
1111111111111111111111111

22222222222222222222222222222222222222
2222222222222

3333333333333333333333333
3333333333333333333333333

444444444
44

5555555555555555555555555

6666666666666666666666666

the output in your second program:
1111111111111111111111111     <-- right


22222222222222222222222222222222222222
2222222222222                 <-- don't go up to make it one                                    line.

3333333333333333333333333
3333333333333333333333333   <-- same problem

444444444
44                          <-- same

5555555555555555555555555

6666666666666666666666666
 
I want to have:


111111111111111111111111


222222222222222222222222222222222222222222222222222


33333333333333333333333333333333333333333333333333

44444444444

5555555555555555555555555

6666666666666666666666666

Thank you for your two codes. It gives me quite a lot idea of how to do it. I am looking at those codes and I am trying to understand the meaning of those weired stuff, like s/\t+////, bit by bit.  My problem is I don't I have any perl reference(I ordered my books and it is coming soon), I need to gauess those thing one by one. Anyway, I start to know much about perl and start to appreciate the beauty of perl.  Thank you for you time and effort for helping me on this problem. I want to give you more points, however, that is all I have. Your time and patient deserve much more than those points I can give you!

Joe