Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, All Perl gurus: It seems to be a trivial question, but I'm still learning perl syntax... Here is the question: try to parse each line of the file, chomp it (get rid of \n), and check to see if this line ending with period ("."), if yes, do nothing; if not, adding period and newline (".\n") to the end and print it out. However, I cannot get the syntax correct. Any help is appreciated.

Replies are listed 'Best First'.
Re: (nrd) Adding periord to the end of line
by newrisedesigns (Curate) on Nov 20, 2002 at 17:32 UTC
    # replace $myline with your variable or $_ if($myline !~ /\.$/){ $myline .= ".\n"; }

    Why do you put the newline back on? Are you sure you want to do that, considering you don't put it back on for the lines without a period?

    If this helps, sign up! Perl Monks welcomes new monks. Post a reply if you need more help.

    John J Reiser
    newrisedesigns.com

      Thanks, John. It works smoothly. Amazingly simple. Actually, I use similar match, but different print. I know what I did wrong before: I use this instead- print $_, ".\n"; (after the match) And it only works for the first instance! Will join this group now.
Re: Adding periord to the end of line
by dingus (Friar) on Nov 20, 2002 at 17:59 UTC
    Assuming you have already opened FILE to the file in question.
    while (<FILE>) { chomp; next if(m/\.$/); print "$_.\n"; }

    Dingus


    Enter any 47-digit prime number to continue.
Re: Adding periord to the end of line
by Nitrox (Chaplain) on Nov 20, 2002 at 17:25 UTC
    Can you post a snippet of what you have so far?

    -Nitrox

Re: Adding periord to the end of line
by fletcher_the_dog (Friar) on Nov 20, 2002 at 22:42 UTC
    If FILE is the file in question and you have opened it, this should work:
    while (<FILE>) { chomp; /\.$/ or print $_.".\n"; }