Tokenizing with Perl and Unstructured data
I have the following data (from a text file), I would like to split / get each element, and even those element that are blanks (some grades as you can see are not listed, which means they are 0, so I want to get them also)
CRN SUB CRSE SECT COURSE TITLE INSTRUCTOR A A- B+ B B- C+ C C- D+ D D- F I CR NC W WN INV TOTAL
----- -- ---- ---- ----------------- ----------------- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- -----
33450 XX 9950 AIP OVERSEAS-AIP SPAI NOT FOUND 1 1 2
33092 XX 9950 ALB ddddddd, SPN. vi NOT FOUND 1 1
33494 XX 9950开发者_JAVA百科 W16 OVERSEAS Univ.Wes NOT FOUND 1 1
INSTRUCTOR TOTALS NOT FOUND 2 1 18 1 2 24
PERCENTAGE DISTRI NOT FOUND 8 4 75 4 8 ******
33271 PE 3600 001 Global Geography sfnfbg,dsdassaas 2 2 1 1 2 3 6 5 3 3 1 29
INSTRUCTOR TOTALS snakdi,plid 2 2 1 1 2 3 6 5 3 3 1 29
PERCENTAGE DISTRI krapsta,lalalal 7 7 3 3 7 10 21 17 10 10 3 ***
The problem as you can see, I don't have a specific delimiter, because some grades are missing, if they weren't, I could have getting all the data from the line start until the first grade ('A') and then all the grades and splitting them by /\s+/, but thats not the case. any suggestions (if there are any....) would be awesome.
thanks,
There are irregularities at places in some columns (note that first total values 18 and 75 are partially in next column), but if you don't need them, you can try something like this:
my @data;
# skip header
my $hdr = <DATA>;
my $sep = <DATA>;
while(<DATA>) {
chomp;
# skip empty and total lines
next if /^\s*$/ || /^[ ]{5}/;
push @data, [
map { s/^\s+//; s/\s+$//; $_ } # trim each column
unpack 'A6A7A7A7 A18A20 A4A4A4A4A4A4A4A4A4A4A4A4A4A4A4A4A4A4 A10', $_
];
}
use Data::Dump;
dd \@data;
__DATA__
CRN SUB CRSE ...
----- -- ---- ...
You might need to tweak column boundaries in unpack template for real data, but this should get you started.
This looks like it would be best to write or find a column-based text parser? I have found DataExtract-FixedWidth on CPAN, but have no personal experience with it. The format looks pretty messy, especially with the numbers on the column borders. You would have to do some kind of pre-processing or heuristics anyway…
精彩评论