Hi John, I believe you know that in Perl there are more than one way to do it! i.e solve a problem. And that one way is no better than the other, it only depend on what the programmer preferred to use, as long as the syntax are correct. Secondly, most of your why would have been answered, if only you check Ryan Lagola's request.
>>If @ARGV contains more than one element then this will not work correctly. *Not true!* using $ARGV[0], select only one file to generate your report from. But using @ARGV, one has all the files listed. Moreover, how do you know how many files he/she intended using from the CLI at once?! So, for me it is saver to use @ARGV. Please, don't misunderstand this, there are several ways of doing things! " > 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? " I don't know what you mean by "copying both $raw_file and $filename into a string"! If you mean by using a double quote around $raw_file and $filename, then I should explain that that is called Interpolation in Perl! -- These two variables are scalar, so when a double quote they are interpolated i.e the value of the scalar (in this context) is inserted. Lastly, codes are written to be improved on. One of the reasons we have different books! Regards. On Thu, Aug 4, 2011 at 6:21 PM, John W. Krahn <jwkr...@shaw.ca> wrote: > 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/ > > >