A few points about your second script.

It's always a good idea to use strict and warnings like you have in your first script.

It's also a good idea to use the three argument open, again, as you have in your first script. I also prefer to use a lexical file handle (see the docs).

Under strict and warnings

my @columns = (category,id,code,title,summary);
gives a "Bareword not allowed" warning. The Perl quoted word function is useful in this case:
my @columns = qw{ category id code title summary prereq group subgroup sequence rolemandatory rolerecommended roleoptional url modality length };
And blow some white space in there while we're about it. :-)

If you think about your column names as keys and the fields as values then it's worth considering a hash. This looks a bit odd but it's a hash slice which populates the hash keys from @columns using the fields as values.

@rec{@columns} = split ('\|', $row);
You can then build your record like this:
printf $fh_out "<xtag:%s>%s</xtag:%s>\n", $colname, $rec{$colname}, $colname;
Here we're using printf to make formating the string a bit easier to see. Because we have a hash we don't need your symbolic reference ${$colname} which is best avoided and won't work under strict and warnings anyhows.

Other monks will likely suggest better ways to work with xml (it's not my forte) but if you are going to do it like this I hope my comments are of some use.

Here's my crack at it.
#!/usr/bin/perl use strict; use warnings; # $IN = "someFileName.txt"; my $OUT = "someOutFile.xml"; open (my $fh_out, '>', $OUT) or die "can't open $OUT to write: $!"; #open(IN) or die ("Cannot open file ($IN)"); print $fh_out <<TOP; <?xml version="1.0" encoding="ISO-8859-1"?> <xtags:some-node xml:lang="en"> TOP my @columns = qw{ category id code title summary prereq group subgroup sequence rolemandatory rolerecommended roleoptional url modality length }; my $id; foreach my $row (<DATA>){ chomp $row; print $fh_out "\t<star:course>\n"; my %rec; @rec{@columns} = split ('\|', $row); for my $colname (@columns){ printf $fh_out "\t\t<xtag:%s>%s</xtag:%s>\n", $colname, $rec{$colname}, $colname; } print $fh_out "\t</xtag:course>\n"; $id++; } print $fh_out "</star:learning-paths>\n"; print "$id records\n"; __DATA__ a|a|a|a|a|a|a|a|a|a|a|a|a|a|a b|b|b|b|b|b|b|b|b|b|b|b|b|b|b c|c|c|c|c|c|c|c|c|c|c|c|c|c|c d|d|d|d|d|d|d|d|d|d|d|d|d|d|d e|e|e|e|e|e|e|e|e|e|e|e|e|e|e f|f|f|f|f|f|f|f|f|f|f|f|f|f|f g|g|g|g|g|g|g|g|g|g|g|g|g|g|g
update 1: added some output.
update 2: added chomp
update 3: added some tabs to the output
<?xml version="1.0" encoding="ISO-8859-1"?> <xtags:some-node xml:lang="en"> <star:course> <xtag:category>a</xtag:category> <xtag:id>a</xtag:id> <xtag:code>a</xtag:code> <xtag:title>a</xtag:title> <xtag:summary>a</xtag:summary> <xtag:prereq>a</xtag:prereq> <xtag:group>a</xtag:group> <xtag:subgroup>a</xtag:subgroup> <xtag:sequence>a</xtag:sequence> <xtag:rolemandatory>a</xtag:rolemandatory> <xtag:rolerecommended>a</xtag:rolerecommended> <xtag:roleoptional>a</xtag:roleoptional> <xtag:url>a</xtag:url> <xtag:modality>a</xtag:modality> <xtag:length>a</xtag:length> </xtag:course> <star:course> <xtag:category>b</xtag:category> <xtag:id>b</xtag:id> <xtag:code>b</xtag:code> <xtag:title>b</xtag:title> <xtag:summary>b</xtag:summary> <xtag:prereq>b</xtag:prereq> <xtag:group>b</xtag:group> <xtag:subgroup>b</xtag:subgroup> <xtag:sequence>b</xtag:sequence> <xtag:rolemandatory>b</xtag:rolemandatory> <xtag:rolerecommended>b</xtag:rolerecommended> <xtag:roleoptional>b</xtag:roleoptional> <xtag:url>b</xtag:url> <xtag:modality>b</xtag:modality> <xtag:length>b</xtag:length> </xtag:course>

In reply to Re: Bring two scripts together? by wfsp
in thread Bring two scripts together? by lakeTrout

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.