PureBytes Links
Trading Reference Links
|
>My file is called FILE1.PRN
>Specs:
> 1. First 10 columns for stock or ticker name
> 2. 3 spaces
>3. 8 columns for Open
> 4. 3 spaces
> 5. 8 columns for High
> 6. 3 spaces
> 7. 8 columns for Low
> 8. 3 spaces
> 9. 8 columns for Close
>10. 3 spaces
>11. 9 columns for Volume
This is a fixed width data file, which lends itself very well to import
into Excel or Access and then export to a comma delimited file using a
macro for import via the Downloader or other tool. However, here is the
way I would handle the file using Perl. The trick here is to find a
regular expression to split the data.
>This is daily data file, however what_data should be variable, where you
>could set it to Daily, Weekly or Monthly
>and then recompile your program for that variable. In this ex. var
>what_data=D
Perl is interpreted, not compiled. In any case it is a simple matter to supply
a command line argument.
>Let's say my input looks like that:
>
>WESTERN_C1 136.78 156.89 160.44 200.71 222999333
>
>WESTERN_C2 136.78 156.89 160.44 200.71 222999333
>
>WESTERN_C3 136.78 156.89 160.44 200.71 222999333
>
>WESTERN_C4 136.78 156.89 160.44 200.71 222999333
Seems you have broken your own rules here since your file description does not
match what is displayed. Here we have a file which has plenty of whitespace
which could be spaces, tabs, carriage returns, etc. We know from the
description
that the file should have one record per row, each field of fixed length and
separated by 3 spaces. Let's use a routine that splits the data apart on white
space with the stipulation that the ticker does not contain whitespace (we can
modify our regular expression to handle that if necessary):
#!/usr/local/bin/perl
# usage: perl parse.pl inputFile outputFile dataType date
# guess what? Hash(#)=comments, shebang (#!) is special
($InputFile,$OutputFile,$DataType,$thisDate)=@xxxx;
open (INFILE, "$InputFile") || die ("Input File not found");
open (OUTFILE, ">$OutputFile") || die("Can't open output file");
until (eof(INFILE)) {
$thisQuote = <INFILE>; # read a line
chop ($thisQuote); # remove end of line char
($t,$o,$h,$l,$c,$vol)=split(/[\s]+/,$thisQuote); # [1]
if (length($t)>0) { # check for bad line [2]
print OUTFILE ("$t,$o,$h,$l,$c,$vol\n");
}
}
close INFILE;
close OUTFILE;
#[1] split the line where there are at least one whitespace character [\s]+ and
load result into variables
#[2] use any kind of test here that is relevant
Notice I didn't do anything with $DataType, but you would just include
it in the print statement where needed. You could also either make the
date a command line argument or calculate it with a Perl function. This
isn't intended to be a perfect solution, just an example.
How about some examples from a Web site?
Cheers,
Jim
|