libmonk has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
Please don't judge me too harshly as a perl newbie but I am a bit lost in the perl forest. I am attempting I/O on a tab delimited file which looks like this:
ISBN OCLC TITLE AUTHOR CALL_NUMBER
853694893 53123369 Transdermal and topical drug delivery from theory to clinical practice / Adrian Williams. "Williams, Adrian, 1963-" RM151 .W55 2003X 2003
471056693 "Pattern classification / Richard O. Duda, Peter E. Hart and David G. Stork." "Duda, Richard O." 006.4; D844; 2001
I've cobbled together code from some old threads but the parser is not pleased. I'm sure there are some elementary mistakes. Would you please help me clean it up?
Thanks,
Libmonk
#!/usr/bin/perl
use strict;
use warnings;
use IO::File;
use LWP::Simple;
## file's structure - tab delimited
# ISBN OCLC TITLE AUTHOR CALL_NUMBER
open (DATAFILE, "input.txt") or die "There is a problem opening the datafile.";
open (NEWFILE, "output.txt") or die "There is a problem opening the output file.";
while (<NEWFILE>) {
chomp;
my @fields = split(/\t/, $line); # splits tab separated fields - replace \t with \, \^ \|, whatever you need
# field names
$isbn = $fields[0];
$ocln = $fields1;
$title = $fields2;
$author = $fields3;
$call_number = $fields4;
}
foreach $line (<DATAFILE>)
{
$line =~ s/&+/&/;
($isbn, $ocln, $title, $author, $call_number) = split(/\t/, $line);
#$ISBN =~ /(^\d{10,13})/;
#$ISBN = $1;
print NEWFILE "<match>\n";
print NEWFILE "<title>";
print NEWFILE $title, "</title>\n";
print NEWFILE "<isbn>";
print NEWFILE $isbn, "</isbnr>\n";
print NEWFILE "<call_number>";
print NEWFILE $call_number,"</call_number>";
print NEWFILE "</match>\n";
}
close (DATAFILE);
close (NEWFILE);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: input tab delimited file
by ELISHEVA (Prior) on Jul 14, 2009 at 16:49 UTC | |
by libmonk (Initiate) on Jul 14, 2009 at 18:25 UTC | |
by graff (Chancellor) on Jul 15, 2009 at 01:33 UTC | |
by Marshall (Canon) on Jul 15, 2009 at 09:36 UTC | |
|
Re: input tab delimited file
by ropey (Hermit) on Jul 14, 2009 at 16:24 UTC | |
|
Re: input tab delimited file
by moritz (Cardinal) on Jul 14, 2009 at 15:53 UTC | |
by Bloodnok (Vicar) on Jul 14, 2009 at 17:02 UTC |