timothy adigun wrote:
Hi Ryan,
Try the the code below, it should help.

==========<CODE>=============

#!/usr/bin/perl -w
use strict;

  my $ln="";
  my ($yr,$cat,$win)=("","","");
  my $filename="New_output.txt";
  chomp(my $raw_file=<@ARGV>);

That is the same as saying:

chomp( my $raw_file = glob "@ARGV" );

Why are you copying the contents of @ARGV to a string and then globbing that string?

If @ARGV contains more than one element then this will not work correctly.

And why chomp() a string that will not contain newlines?

What you want is something like:

my $raw_file = $ARGV[ 0 ];

Or:

my $raw_file = shift;

But you should probably verify that @ARGV is not empty first.


  open READFILE,"<","$raw_file" or die "can't open $!";

Why are you copying $raw_file to a string?


  open OUTPUTFILE,">","$filename" or die "cannot read $!";

Why are you copying $filename to a string?


   while(<READFILE>){chomp;
           $ln.="\n" if /^\W.?+$/;
     if(/^\d{4}/){$yr=$&;}  # get the year
     if(/^[A-Z].+/){ $cat=$&; # get the Category
        $cat=join"",split /,/,$cat; # remove the comma in front
      $ln.=" $yr: ".$cat;           # add both the year and Category
     }
     if(/\--.+/){$win=$`;     # get the winner

The use of $&, $' and $` will slow down *ALL* regular expressions in the program. Better to just use capturing parentheses.

    if (/^(\d{4})/ ) { $yr = $1 }  # get the year
    if ( /^([A-Z].+)/ ) {
        $cat = $1; # get the Category
        $cat = join "", split /,/, $cat; # remove the comma in front
        $ln.= " $yr: " . $cat;           # add both the year and Category
    }
    if ( /(.*?)\--.+/ ) { $win = $1;     # get the winner

And the line:

        $cat = join "", split /,/, $cat; # remove the comma in front

Says "remove the comma in front" but it will remove ALL commas.

A more efficient way to remove all commas is:

        $cat =~ tr/,//d; # remove all commas


        $win=join"",split /[\*,\"]/,$win;

Again, a more efficient way to remove all '*', ',' and '"' characters is:

    $win =~ tr/*,"//d;


      $ln.=": ".$win."\n";  ####  If you use "$ln.=": ".$win. $&. "\n";"
      #### you get "-- It Happened One Night {""Peter Warne""}",etc added to
what you have
     }
   }
   print OUTPUTFILE $ln;
close OUTPUTFILE;
close READFILE;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to