[wellylug] Need some help with Perl
sljudd at paradise.net.nz
sljudd at paradise.net.nz
Tue Oct 30 16:23:30 NZDT 2001
Quoting Jamie Dobbs <jamie.dobbs at paradise.net.nz>:
> Sorry about the length of this email but all the code is necessary so
> people can see what I'm doing (Its my first Perl program so please
> don't hassle my 'style' (or lack thereof!) too much)
Well, my approach is to use "chomp" on each line, to get rid of trailing
whitespace. This is easier and quicker than fiddling with pattern. You may also
find "pack/unpack" easier than repeated calls to substr.
I find with these data-munging scripts that I also tend to:
- open input file
- open output file
- while there are lines
- read line
- chomp
- munge
- write an output line or push to a buffer or do whatever
- tidy up file handles and exit
which is a bit gentler on your system if your input file is very large.
Your problem, I think, is that you are adding "\n" on an operating system where
line breaks are carriage-return/linefeed, and that's probably not what the
Tandem system is expecting. Perl uses whatever your OS uses for linefeeds when
you write "\n". Find out what the Tandem expects, and use that.
>
> OK, I have the following code:
>
> !/usr/local/bin/perl
> #This program will process 'run-on' type racing fields
>
> use Shell;
>
> $file = $ARGV[0]; #get the file passed as the command line
> parameter
> open(INFO,$file); #open the file
> @lines=<INFO>; #read the contents of the file into an array
> close(INFO); #close the file
>
> #Set up the 'hash table' for the race venues
> %venues = ( roto => "Rotorua",
> mlbh => "Marlborough",
> wavE => "Waverly",
> otag => "Otago",
> wint => "Winton",
> wodv => "Woodville",
> wynd => "Wyndham" );
>
> #process the 'array'
> $venue = substr $file,4,4;
> $date = (substr $file,0,2).("/").(substr $file,2,2);
> $day = date("-d ", $date," +%a");
> $day = substr $day,0,3;
> #create the 'header' information
> $newlines = "";
> $newlines = $newlines.chr(1)."R RBT FIELDS".chr(19).chr(18).chr(2);
> $newlines = $newlines.chr(9)."$venues{$venue}-$day-FIELDS";
> $newlines = $newlines.chr(9)."$venues{$venue} $day FIELDS"."\n";
> #now process each line of the input array and add it to the newlines
> array for sending
> foreach $line (@lines) #visit each line in turn and call it $line
> {
> $lines =~ s/\n//g;
> $lines =~ s/\r//g;
> $lines =~ s/cM//g;
> #if the line starts with RACE put a blank line before it
> if ($line =~ /^ RACE/)
> {
> $newlines = $newlines." \n\t".$line;
> }
> #if the line ends with , or ) print it with a trailing space
> elsif ($line =~ /,$/ | $line =~ /\)$/)
> {
> $newlines = $newlines.$line." ";
> }
> #if the line starts with a digit print it with a leading TAB
> elsif ($line =~ /^ \d/)
> {
> $newlines = $newlines."\t".$line;
> }
> #if the line starts with a non alphanumeric character print
> it with a
> #leading TAB (eg. race purse figure has $ at start)
> #also set flag to indicate next section is runon copy
> elsif ($line =~ /^ \W/)
> {
> $newlines = $newlines."\t".$line;
> $flag = "\t";
> }
> #if the word TAB appears preceed it with a TAB character
> elsif ($line =~ /^ TAB/)
> {
> $newlines = $newlines."\t".$line;
> }
> #if none of the above then print it without a TAB in front
> else
> {
> $newlines = $newlines.$flag.$line;
> $flag = "";
> }
> }
> $newlines = $newlines."\nNZPA WGT".chr(4);
> #Now output the processed array to a file ready to send to Tandem
> $ofile = "$file".".txt"; #create a new file with a .txt
> extension
> open(INFO,">$ofile"); #open the file for output
> print INFO $newlines; #output the new 'array' to the file
> close(INFO);
>
> It processes the input file fine and puts it into a file once all the
> 'modifications' have been done. The problem is though that the output
> files has an 0D 0D 0A sequence at the end of everyline which makes it
> appear double line spaced when I send it to our mainframe.
> Can anyone help me out and give me a method whereby I can remove the
> extra line between each line output?
>
> TIA
>
> Jamie
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.291 / Virus Database: 156 - Release Date: 25/10/2001
>
>
> ------------------------ Yahoo! Groups Sponsor
>
> .-. Wellington
> /V\ Linux
> // \\ Users
> /( )\ Group
> ^^-^^
> http://wlug.paradise.net.nz/
>
> To unsubscribe from this group, send an email to:
> wellylug-unsubscribe at egroups.com
>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>
>
More information about the wellylug
mailing list