Wow, Perl is really cool when it works! Thanks for your help!!

Here's my code:

#!/usr/bin/perl -w
use strict;

open(INPUTFILE, "< input.txt") or die "cannot open file for reading $!";
open(OUTPUTFILE, "> output.txt") or die "cannot open file for writing $!";

foreach my $line (<INPUTFILE>)
{
$line =~ s/&+/&amp;/;

# you can also use my (...) to declare many variables
# at once
my ($isbn, $ocln, $title, $author, $call_number, $publish_date, $lccn, $request_number)
= split(/\t/, $line);

# you'll likely have different defaults for cases where
# fields are undefined
$isbn='' unless defined($isbn);
$ocln='' unless defined($ocln);
$title='' unless defined($title);
$author='' unless defined($author);
$call_number='' unless defined($call_number);
$publish_date='' unless defined($publish_date);
$lccn='' unless defined($lccn);
$request_number='' unless defined($request_number);

print OUTPUTFILE "<match>\n";
print OUTPUTFILE "<title>";
print OUTPUTFILE $title, "</title>\n";
print OUTPUTFILE "<isbn>";
print OUTPUTFILE $isbn, "</isbn>\n";
print OUTPUTFILE "<call_number>";
print OUTPUTFILE $call_number,"</call_number>";
print OUTPUTFILE "</match>\n";
}

close INPUTFILE;
close OUTPUTFILE;

Here's a sample of the output:

$ tail output.txt
<isbn>313228841</isbn>
<call_number>Z8424.D69</call_number></match>
<match>
<title>"Blogs, Wikipedia, Second life, and Beyond : from production to produsage / Axel Bruns."</title>
<isbn>820488674</isbn>
<call_number>ZA4482 .B78 2008</call_number></match>
<match>
<title>"Living standards in the past : new perspectives on well-being in Asia and Europe / edited by Robert C. Allen, Tommy Bengtsson, and Martin Dribe."</title>
<isbn>199280681</isbn>
<call_number>zHD7048.L58 2005</call_number></match>

Thanks again! Now I'm off to build on this beginning ...

Best,

Libmonk


In reply to Re^2: input tab delimited file by libmonk
in thread input tab delimited file by libmonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.